Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,20 @@ describe('when evaluating user equivalent contexts for segments', () => {
expect(res.detail.value).toBe(false);
});

it('can negate a segment match op', async () => {
const segment = {
key: 'test',
included: ['foo'],
version: 1,
};
const evaluator = new Evaluator(basicPlatform, new TestQueries({ segments: [segment] }));
const flag = makeFlagWithSegmentMatch(segment);
flag.rules[0].clauses![0].negate = true;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically this should make the results of this test opposite of the above test.

const user = { key: 'bar' };
const res = await evaluator.evaluate(flag, Context.fromLDContext(user));
expect(res.detail.value).toBe(true);
});

it.each([basicUser, basicSingleKindUser, basicMultiKindUser])(
'matches segment with user who is both included and excluded',
async (context) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/shared/sdk-server/src/evaluation/Evaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import ErrorKinds from './ErrorKinds';
import EvalResult from './EvalResult';
import evalTargets from './evalTargets';
import makeBigSegmentRef from './makeBigSegmentRef';
import matchClauseWithoutSegmentOperations from './matchClause';
import matchClauseWithoutSegmentOperations, { maybeNegate } from './matchClause';
import matchSegmentTargets from './matchSegmentTargets';
import { Queries } from './Queries';
import Reasons from './Reasons';
Expand Down Expand Up @@ -296,7 +296,7 @@ export default class Evaluator {
return new MatchError(errorResult);
}

return new Match(match);
return new Match(maybeNegate(clause, match));
}
// This is after segment matching, which does not use the reference.
if (!clause.attributeReference.isValid) {
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/sdk-server/src/evaluation/matchClause.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Context } from '@launchdarkly/js-sdk-common';
import { Clause } from './data/Clause';
import Operators from './Operations';

function maybeNegate(clause: Clause, value: boolean): boolean {
export function maybeNegate(clause: Clause, value: boolean): boolean {
if (clause.negate) {
return !value;
}
Expand Down