Skip to content

Commit

Permalink
tests for Rules class
Browse files Browse the repository at this point in the history
  • Loading branch information
ForestOfLight committed Jan 26, 2025
1 parent eccefb5 commit 21c9914
Show file tree
Hide file tree
Showing 4 changed files with 321 additions and 53 deletions.
16 changes: 6 additions & 10 deletions Canopy [BP]/scripts/lib/canopy/Rule.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { world } from '@minecraft/server';
import IPC from "../ipc/ipc";
import Rules from "./Rules";

export class Rule {
#category;
Expand All @@ -19,11 +18,6 @@ export class Rule {
this.#contingentRules = contingentRules;
this.#independentRules = independentRules;
this.#extensionName = extensionName;
if (Rules.exists(identifier)) {
console.warn(`[Canopy] Rule with identifier '${identifier}' already exists.`);
return;
}
Rules.add(this);
}

getCategory() {
Expand Down Expand Up @@ -61,6 +55,12 @@ export class Rule {
return this.parseValue(world.getDynamicProperty(this.#identifier));
}

getNativeValue() {
if (this.#extensionName)
throw new Error(`[Canopy] [Rule] Native value is not available for ${this.#identifier} from extension ${this.#extensionName}.`);
return this.parseValue(world.getDynamicProperty(this.#identifier));
}

parseValue(value) {
try {
return JSON.parse(value);
Expand All @@ -79,10 +79,6 @@ export class Rule {
world.setDynamicProperty(this.#identifier, value);
}
}

getDependentRuleIDs() {
return Rules.getAll().filter(rule => rule.#contingentRules.includes(this.#identifier)).map(rule => rule.#identifier);
}
}

export default Rule;
45 changes: 33 additions & 12 deletions Canopy [BP]/scripts/lib/canopy/Rules.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,63 @@
import { world } from "@minecraft/server";

export class Rules {
static #rules = {};
static rules = {};

static add(rule) {
this.#rules[rule.getID()] = rule;
if (this.exists(rule.getID())) {
throw new Error(`[Canopy] Rule with identifier '${rule.getID()}' already exists.`);
}
this.rules[rule.getID()] = rule;
}

static get(identifier) {
return this.#rules[identifier];
return this.rules[identifier];
}

static getAll() {
return Object.values(this.#rules);
return Object.values(this.rules);
}

static getIDs() {
return Object.keys(this.rules);
}

static exists(name) {
return this.#rules[name] !== undefined;
return this.rules[name] !== undefined;
}

static remove(name) {
delete this.#rules[name];
delete this.rules[name];
}

static clear() {
this.#rules = {};
this.rules = {};
}

static async getValue(identifier) {
return await this.get(identifier).getValue();
const rule = this.get(identifier);
if (!rule)
throw new Error(`[Canopy] Rule with identifier '${identifier}' does not exist.`);
return await rule.getValue();
}

static getNativeValue(identifier) {
return this.get(identifier).parseString(world.getDynamicProperty(identifier));
const rule = this.get(identifier);
if (!rule)
throw new Error(`[Canopy] Rule with identifier '${identifier}' does not exist.`);
return rule.getNativeValue();
}

static setValue(identifier, value) {
this.get(identifier).setValue(value);
const rule = this.get(identifier)
if (!rule)
throw new Error(`[Canopy] Rule with identifier '${identifier}' does not exist.`);
rule.setValue(value);
}

static getDependentRuleIDs(identifier) {
const rule = this.get(identifier);
if (!rule)
throw new Error(`[Canopy] Rule with identifier '${identifier}' does not exist.`);
return Rules.getAll().filter(rule => rule.getContigentRuleIDs().includes(identifier)).map(rule => rule.getID());
}
}

Expand Down
73 changes: 42 additions & 31 deletions __tests__/BP/scripts/lib/canopy/Rule.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,60 @@ vi.mock('@minecraft/server', () => ({
}));

describe('Rule', () => {
let testRule;
beforeEach(() => {
Rules.clear();
new Rule({
testRule = new Rule({
category: 'test',
identifier: 'test_rule',
description: 'This is a test rule',
extensionName: 'Test Extension',
contingentRules: ['test_rule_2'],
independentRules: ['test_rule_3']
});
Rules.add(testRule);
});

describe('constructor', () => {
console.warn = vi.fn();
it('should add the rule to the Rules registry', () => {
expect(Rules.exists('test_rule')).toBe(true);
it('should initialize with the correct properties', () => {
const ruleData = {
category: 'test_category',
identifier: 'test_identifier',
description: 'test_description',
contingentRules: ['rule1', 'rule2'],
independentRules: ['rule3', 'rule4'],
extensionName: 'test_extension'
};
const rule = new Rule(ruleData);

expect(rule.getCategory()).toBe(ruleData.category);
expect(rule.getID()).toBe(ruleData.identifier);
expect(rule.getDescription()).toEqual({ text: ruleData.description });
expect(rule.getContigentRuleIDs()).toEqual(ruleData.contingentRules);
expect(rule.getIndependentRuleIDs()).toEqual(ruleData.independentRules);
expect(rule.getExtensionName()).toBe(ruleData.extensionName);
});

it('should warn if a rule with the same identifier already exists', () => {
new Rule({ category: 'test', identifier: 'test_rule' });
expect(console.warn).toHaveBeenCalled();
it('should set description to a rawtext object if it is a string', () => {
const rule = new Rule({
category: 'test_category',
identifier: 'test_identifier',
description: 'test_description'
});

expect(rule.getDescription()).toEqual({ text: 'test_description' });
});

it('should not add the rule to the Rules registry if a rule with the same identifier already exists', () => {
new Rule({ category: 'test', identifier: 'test_rule' });
expect(Rules.getAll().length).toBe(1);
it('should set default values for optional properties', () => {
const rule = new Rule({
category: 'test_category',
identifier: 'test_identifier'
});

expect(rule.getDescription()).toEqual({ text: '' });
expect(rule.getContigentRuleIDs()).toEqual([]);
expect(rule.getIndependentRuleIDs()).toEqual([]);
expect(rule.getExtensionName()).toBe(false);
});
});

Expand Down Expand Up @@ -90,6 +118,10 @@ describe('Rule', () => {
// Gametest
});

describe.skip('getNativeValue()', () => {
// Gametest
});

describe('parseValue()', () => {
it('should parse JSON strings to objects', () => {
expect(Rules.get('test_rule').parseValue('{"test": "value"}')).toEqual({ test: 'value' });
Expand Down Expand Up @@ -117,25 +149,4 @@ describe('Rule', () => {
describe.skip('setValue()', () => {
// Gametest
});

describe('getDependentRuleIDs()', () => {
beforeEach(() => {
new Rule({
category: 'test',
identifier: 'test_rule_2',
description: 'This is another test rule',
extensionName: 'Test Extension',
contingentRules: [],
independentRules: []
});
});

it('should return an array of rule IDs that are dependent on the rule', () => {
expect(Rules.get('test_rule_2').getDependentRuleIDs()).toEqual(['test_rule']);
});

it('should return an empty array if no rules are dependent on the rule', () => {
expect(Rules.get('test_rule').getDependentRuleIDs()).toEqual([]);
});
});
});
Loading

0 comments on commit 21c9914

Please sign in to comment.