Skip to content

Commit

Permalink
Update tests to TS
Browse files Browse the repository at this point in the history
  • Loading branch information
balloob committed Nov 6, 2018
1 parent 019367c commit 960ef88
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
18 changes: 9 additions & 9 deletions src/common/entity/entity_filter.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import computeDomain from "./compute_domain.js";
import computeDomain from "./compute_domain";

export type FilterFunc = (entityId: string) => boolean;

export const generateFilter = (
includeDomains: string[],
includeEntities: string[],
excludeDomains: string[],
excludeEntities: string[]
includeDomains?: string[],
includeEntities?: string[],
excludeDomains?: string[],
excludeEntities?: string[]
): FilterFunc => {
const includeDomainsSet = new Set(includeDomains);
const includeEntitiesSet = new Set(includeEntities);
Expand Down Expand Up @@ -37,8 +37,8 @@ export const generateFilter = (

// Case 4 - both includes and excludes specified
// Case 4a - include domain specified
// - if domain is included, and entity not excluded, pass
// - if domain is not included, and entity not included, fail
// - if domain is included, pass if entity not excluded
// - if domain is not included, pass if entity is included
// note: if both include and exclude domains specified,
// the exclude domains are ignored
if (includeDomainsSet.size) {
Expand All @@ -49,8 +49,8 @@ export const generateFilter = (
}

// Case 4b - exclude domain specified
// - if domain is excluded, and entity not included, fail
// - if domain is not excluded, and entity not excluded, pass
// - if domain is excluded, pass if entity is included
// - if domain is not excluded, pass if entity not excluded
if (excludeDomainsSet.size) {
return (entityId) =>
excludeDomainsSet.has(computeDomain(entityId))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { generateFilter } from "../../../src/common/entity/entity_filter.ts";
import { generateFilter } from "../../../src/common/entity/entity_filter";

const assert = require("assert");
import * as assert from "assert";

describe("EntityFilter", () => {
// case 1
Expand All @@ -14,7 +14,7 @@ describe("EntityFilter", () => {

// case 2
it("allows whitelisting entities by entity id", () => {
const filter = generateFilter(null, ["light.kitchen"]);
const filter = generateFilter(undefined, ["light.kitchen"]);

assert(filter("light.kitchen"));
assert(!filter("light.living_room"));
Expand All @@ -29,22 +29,26 @@ describe("EntityFilter", () => {

// case 3
it("allows blacklisting entities by entity id", () => {
const filter = generateFilter(null, null, null, ["light.kitchen"]);
const filter = generateFilter(undefined, undefined, undefined, [
"light.kitchen",
]);

assert(!filter("light.kitchen"));
assert(filter("light.living_room"));
});

it("allows blacklisting entities by domain", () => {
const filter = generateFilter(null, null, ["switch"]);
const filter = generateFilter(undefined, undefined, ["switch"]);

assert(!filter("switch.bla"));
assert(filter("light.kitchen"));
});

// case 4a
it("allows whitelisting domain and blacklisting entity", () => {
const filter = generateFilter(["switch"], null, null, ["switch.kitchen"]);
const filter = generateFilter(["switch"], undefined, undefined, [
"switch.kitchen",
]);

assert(filter("switch.living_room"));
assert(!filter("switch.kitchen"));
Expand All @@ -61,15 +65,20 @@ describe("EntityFilter", () => {

// case 4b
it("allows blacklisting domain and whitelisting entity", () => {
const filter = generateFilter(null, ["switch.kitchen"], ["switch"]);
const filter = generateFilter(undefined, ["switch.kitchen"], ["switch"]);

assert(filter("switch.kitchen"));
assert(!filter("switch.living_room"));
assert(filter("sensor.bla"));
});

it("allows blacklisting domain and excluding entities", () => {
const filter = generateFilter(null, null, ["switch"], ["light.kitchen"]);
const filter = generateFilter(
undefined,
undefined,
["switch"],
["light.kitchen"]
);

assert(!filter("switch.living_room"));
assert(!filter("light.kitchen"));
Expand All @@ -78,7 +87,7 @@ describe("EntityFilter", () => {

// case 4c
it("allows whitelisting entities", () => {
const filter = generateFilter(null, ["light.kitchen"]);
const filter = generateFilter(undefined, ["light.kitchen"]);

assert(filter("light.kitchen"));
assert(!filter("switch.living_room"));
Expand Down

0 comments on commit 960ef88

Please sign in to comment.