stigmergy 1.0.93 → 1.0.95
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/stigmergy +26 -12
- package/bin/stigmergy.cmd +1 -1
- package/docs/HASH_TABLE.md +83 -0
- package/docs/WEATHER_PROCESSOR_API.md +230 -0
- package/docs/best_practices.md +135 -0
- package/docs/development_guidelines.md +392 -0
- package/docs/requirements_specification.md +148 -0
- package/docs/system_design.md +314 -0
- package/docs/tdd_implementation_plan.md +384 -0
- package/docs/test_report.md +49 -0
- package/examples/calculator-example.js +72 -0
- package/examples/json-validation-example.js +64 -0
- package/examples/rest-client-example.js +52 -0
- package/package.json +21 -17
- package/scripts/post-deployment-config.js +9 -2
- package/src/auth.js +171 -0
- package/src/auth_command.js +195 -0
- package/src/calculator.js +220 -0
- package/src/core/cli_help_analyzer.js +625 -562
- package/src/core/cli_parameter_handler.js +121 -0
- package/src/core/cli_tools.js +89 -0
- package/src/core/error_handler.js +307 -0
- package/src/core/memory_manager.js +76 -0
- package/src/core/smart_router.js +133 -0
- package/src/deploy.js +50 -0
- package/src/main_english.js +643 -720
- package/src/main_fixed.js +1035 -0
- package/src/utils.js +529 -0
- package/src/weatherProcessor.js +205 -0
- package/test/calculator.test.js +215 -0
- package/test/collision-test.js +26 -0
- package/test/csv-processing-test.js +36 -0
- package/test/e2e/claude-cli-test.js +128 -0
- package/test/e2e/collaboration-test.js +75 -0
- package/test/e2e/comprehensive-test.js +431 -0
- package/test/e2e/error-handling-test.js +90 -0
- package/test/e2e/individual-tool-test.js +143 -0
- package/test/e2e/other-cli-test.js +130 -0
- package/test/e2e/qoder-cli-test.js +128 -0
- package/test/e2e/run-e2e-tests.js +73 -0
- package/test/e2e/test-data.js +88 -0
- package/test/e2e/test-utils.js +222 -0
- package/test/hash-table-demo.js +33 -0
- package/test/hash-table-test.js +26 -0
- package/test/json-validation-test.js +164 -0
- package/test/rest-client-test.js +56 -0
- package/test/unit/calculator-full.test.js +191 -0
- package/test/unit/calculator-simple.test.js +96 -0
- package/test/unit/calculator.test.js +97 -0
- package/test/unit/cli_parameter_handler.test.js +116 -0
- package/test/weather-processor.test.js +104 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
const { HashTable } = require('../src/utils');
|
|
2
|
+
|
|
3
|
+
// Create a new hash table
|
|
4
|
+
const ht = new HashTable(17);
|
|
5
|
+
|
|
6
|
+
// Test setting values
|
|
7
|
+
ht.set("maroon", "#800000");
|
|
8
|
+
ht.set("yellow", "#FFFF00");
|
|
9
|
+
ht.set("olive", "#808000");
|
|
10
|
+
ht.set("salmon", "#FA8072");
|
|
11
|
+
ht.set("lightcoral", "#F08080");
|
|
12
|
+
ht.set("mediumvioletred", "#C71585");
|
|
13
|
+
ht.set("plum", "#DDA0DD");
|
|
14
|
+
|
|
15
|
+
// Test getting values
|
|
16
|
+
console.log(ht.get("maroon")); // #800000
|
|
17
|
+
console.log(ht.get("yellow")); // #FFFF00
|
|
18
|
+
console.log(ht.get("invalid")); // undefined
|
|
19
|
+
|
|
20
|
+
// Test keys method
|
|
21
|
+
console.log(ht.keys());
|
|
22
|
+
|
|
23
|
+
// Test values method
|
|
24
|
+
console.log(ht.values());
|
|
25
|
+
|
|
26
|
+
console.log("Hash table tests completed successfully!");
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
const { parseAndValidateJSON } = require('../src/utils');
|
|
2
|
+
|
|
3
|
+
// Test cases for parseAndValidateJSON function
|
|
4
|
+
console.log('Testing parseAndValidateJSON function...\n');
|
|
5
|
+
|
|
6
|
+
// Test 1: Valid JSON without schema
|
|
7
|
+
try {
|
|
8
|
+
const jsonData = '{"name": "John", "age": 30}';
|
|
9
|
+
const result = parseAndValidateJSON(jsonData);
|
|
10
|
+
console.log('Test 1 passed: Valid JSON parsed successfully');
|
|
11
|
+
console.log('Result:', result);
|
|
12
|
+
} catch (error) {
|
|
13
|
+
console.log('Test 1 failed:', error.message);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
console.log();
|
|
17
|
+
|
|
18
|
+
// Test 2: Invalid JSON
|
|
19
|
+
try {
|
|
20
|
+
const invalidJson = '{"name": "John", "age":}';
|
|
21
|
+
const result = parseAndValidateJSON(invalidJson);
|
|
22
|
+
console.log('Test 2 failed: Should have thrown an error');
|
|
23
|
+
} catch (error) {
|
|
24
|
+
console.log('Test 2 passed: Correctly rejected invalid JSON');
|
|
25
|
+
console.log('Error:', error.message);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
console.log();
|
|
29
|
+
|
|
30
|
+
// Test 3: Valid JSON with schema validation
|
|
31
|
+
try {
|
|
32
|
+
const jsonData = '{"name": "John", "age": 30, "active": true}';
|
|
33
|
+
const schema = {
|
|
34
|
+
required: ['name', 'age'],
|
|
35
|
+
properties: {
|
|
36
|
+
name: { type: 'string' },
|
|
37
|
+
age: { type: 'number' },
|
|
38
|
+
active: { type: 'boolean' }
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
const result = parseAndValidateJSON(jsonData, schema);
|
|
42
|
+
console.log('Test 3 passed: Valid JSON with schema validation');
|
|
43
|
+
console.log('Result:', result);
|
|
44
|
+
} catch (error) {
|
|
45
|
+
console.log('Test 3 failed:', error.message);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
console.log();
|
|
49
|
+
|
|
50
|
+
// Test 4: Missing required field
|
|
51
|
+
try {
|
|
52
|
+
const jsonData = '{"name": "John"}';
|
|
53
|
+
const schema = {
|
|
54
|
+
required: ['name', 'age'],
|
|
55
|
+
properties: {
|
|
56
|
+
name: { type: 'string' },
|
|
57
|
+
age: { type: 'number' }
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
const result = parseAndValidateJSON(jsonData, schema);
|
|
61
|
+
console.log('Test 4 failed: Should have thrown an error for missing required field');
|
|
62
|
+
} catch (error) {
|
|
63
|
+
console.log('Test 4 passed: Correctly rejected missing required field');
|
|
64
|
+
console.log('Error:', error.message);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
console.log();
|
|
68
|
+
|
|
69
|
+
// Test 5: Wrong data type
|
|
70
|
+
try {
|
|
71
|
+
const jsonData = '{"name": "John", "age": "thirty"}';
|
|
72
|
+
const schema = {
|
|
73
|
+
required: ['name', 'age'],
|
|
74
|
+
properties: {
|
|
75
|
+
name: { type: 'string' },
|
|
76
|
+
age: { type: 'number' }
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
const result = parseAndValidateJSON(jsonData, schema);
|
|
80
|
+
console.log('Test 5 failed: Should have thrown an error for wrong data type');
|
|
81
|
+
} catch (error) {
|
|
82
|
+
console.log('Test 5 passed: Correctly rejected wrong data type');
|
|
83
|
+
console.log('Error:', error.message);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
console.log();
|
|
87
|
+
|
|
88
|
+
// Test 6: Enum validation
|
|
89
|
+
try {
|
|
90
|
+
const jsonData = '{"status": "active"}';
|
|
91
|
+
const schema = {
|
|
92
|
+
required: ['status'],
|
|
93
|
+
properties: {
|
|
94
|
+
status: { type: 'string', enum: ['active', 'inactive', 'pending'] }
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
const result = parseAndValidateJSON(jsonData, schema);
|
|
98
|
+
console.log('Test 6 passed: Valid enum value accepted');
|
|
99
|
+
console.log('Result:', result);
|
|
100
|
+
} catch (error) {
|
|
101
|
+
console.log('Test 6 failed:', error.message);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
console.log();
|
|
105
|
+
|
|
106
|
+
// Test 7: Invalid enum value
|
|
107
|
+
try {
|
|
108
|
+
const jsonData = '{"status": "deleted"}';
|
|
109
|
+
const schema = {
|
|
110
|
+
required: ['status'],
|
|
111
|
+
properties: {
|
|
112
|
+
status: { type: 'string', enum: ['active', 'inactive', 'pending'] }
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
const result = parseAndValidateJSON(jsonData, schema);
|
|
116
|
+
console.log('Test 7 failed: Should have thrown an error for invalid enum value');
|
|
117
|
+
} catch (error) {
|
|
118
|
+
console.log('Test 7 passed: Correctly rejected invalid enum value');
|
|
119
|
+
console.log('Error:', error.message);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
console.log();
|
|
123
|
+
|
|
124
|
+
// Test 8: Array validation
|
|
125
|
+
try {
|
|
126
|
+
const jsonData = '{"tags": ["javascript", "nodejs"]}';
|
|
127
|
+
const schema = {
|
|
128
|
+
required: ['tags'],
|
|
129
|
+
properties: {
|
|
130
|
+
tags: {
|
|
131
|
+
type: 'array',
|
|
132
|
+
items: { type: 'string' }
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
const result = parseAndValidateJSON(jsonData, schema);
|
|
137
|
+
console.log('Test 8 passed: Valid array accepted');
|
|
138
|
+
console.log('Result:', result);
|
|
139
|
+
} catch (error) {
|
|
140
|
+
console.log('Test 8 failed:', error.message);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
console.log();
|
|
144
|
+
|
|
145
|
+
// Test 9: Invalid array items
|
|
146
|
+
try {
|
|
147
|
+
const jsonData = '{"tags": ["javascript", 123]}';
|
|
148
|
+
const schema = {
|
|
149
|
+
required: ['tags'],
|
|
150
|
+
properties: {
|
|
151
|
+
tags: {
|
|
152
|
+
type: 'array',
|
|
153
|
+
items: { type: 'string' }
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
const result = parseAndValidateJSON(jsonData, schema);
|
|
158
|
+
console.log('Test 9 failed: Should have thrown an error for invalid array items');
|
|
159
|
+
} catch (error) {
|
|
160
|
+
console.log('Test 9 passed: Correctly rejected invalid array items');
|
|
161
|
+
console.log('Error:', error.message);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
console.log('\nAll tests completed!');
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
const { RESTClient } = require('../src/utils');
|
|
2
|
+
|
|
3
|
+
async function testRESTClient() {
|
|
4
|
+
console.log('Testing REST API Client...\n');
|
|
5
|
+
|
|
6
|
+
// Test with a public API
|
|
7
|
+
const client = new RESTClient('https://jsonplaceholder.typicode.com');
|
|
8
|
+
|
|
9
|
+
try {
|
|
10
|
+
// Test GET request
|
|
11
|
+
console.log('1. Testing GET request...');
|
|
12
|
+
const getResponse = await client.get('/posts/1');
|
|
13
|
+
console.log('GET Response Status:', getResponse.status);
|
|
14
|
+
console.log('GET Response Data Title:', getResponse.data.title);
|
|
15
|
+
console.log('-------------------\n');
|
|
16
|
+
|
|
17
|
+
// Test POST request
|
|
18
|
+
console.log('2. Testing POST request...');
|
|
19
|
+
const postData = {
|
|
20
|
+
title: 'Test Post',
|
|
21
|
+
body: 'This is a test post',
|
|
22
|
+
userId: 1
|
|
23
|
+
};
|
|
24
|
+
const postResponse = await client.post('/posts', postData);
|
|
25
|
+
console.log('POST Response Status:', postResponse.status);
|
|
26
|
+
console.log('POST Response Data ID:', postResponse.data.id);
|
|
27
|
+
console.log('-------------------\n');
|
|
28
|
+
|
|
29
|
+
// Test PUT request
|
|
30
|
+
console.log('3. Testing PUT request...');
|
|
31
|
+
const putData = {
|
|
32
|
+
id: 1,
|
|
33
|
+
title: 'Updated Post',
|
|
34
|
+
body: 'This post has been updated',
|
|
35
|
+
userId: 1
|
|
36
|
+
};
|
|
37
|
+
const putResponse = await client.put('/posts/1', putData);
|
|
38
|
+
console.log('PUT Response Status:', putResponse.status);
|
|
39
|
+
console.log('PUT Response Data Title:', putResponse.data.title);
|
|
40
|
+
console.log('-------------------\n');
|
|
41
|
+
|
|
42
|
+
// Test DELETE request
|
|
43
|
+
console.log('4. Testing DELETE request...');
|
|
44
|
+
const deleteResponse = await client.delete('/posts/1');
|
|
45
|
+
console.log('DELETE Response Status:', deleteResponse.status);
|
|
46
|
+
console.log('DELETE Request completed successfully');
|
|
47
|
+
console.log('-------------------\n');
|
|
48
|
+
|
|
49
|
+
console.log('All tests passed!');
|
|
50
|
+
} catch (error) {
|
|
51
|
+
console.error('Test failed:', error.message);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Run the test
|
|
56
|
+
testRESTClient();
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unit tests for Calculator class following project patterns
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
const assert = require('assert');
|
|
6
|
+
const Calculator = require('../../src/calculator');
|
|
7
|
+
|
|
8
|
+
describe('Calculator Unit Tests', () => {
|
|
9
|
+
let calc;
|
|
10
|
+
|
|
11
|
+
beforeEach(() => {
|
|
12
|
+
calc = new Calculator();
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
describe('Basic Arithmetic Operations', () => {
|
|
16
|
+
it('should add two numbers correctly', () => {
|
|
17
|
+
assert.strictEqual(calc.add(5, 3), 8);
|
|
18
|
+
assert.strictEqual(calc.add(-2, 7), 5);
|
|
19
|
+
assert.strictEqual(calc.add(0, 0), 0);
|
|
20
|
+
assert.strictEqual(calc.add(1.5, 2.5), 4);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('should subtract two numbers correctly', () => {
|
|
24
|
+
assert.strictEqual(calc.subtract(10, 4), 6);
|
|
25
|
+
assert.strictEqual(calc.subtract(5, 8), -3);
|
|
26
|
+
assert.strictEqual(calc.subtract(0, 0), 0);
|
|
27
|
+
assert.strictEqual(calc.subtract(3.5, 1.5), 2);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('should multiply two numbers correctly', () => {
|
|
31
|
+
assert.strictEqual(calc.multiply(6, 7), 42);
|
|
32
|
+
assert.strictEqual(calc.multiply(-3, 4), -12);
|
|
33
|
+
assert.strictEqual(calc.multiply(0, 100), 0);
|
|
34
|
+
assert.strictEqual(calc.multiply(2.5, 4), 10);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('should divide two numbers correctly', () => {
|
|
38
|
+
assert.strictEqual(calc.divide(15, 3), 5);
|
|
39
|
+
assert.strictEqual(calc.divide(7, 2), 3.5);
|
|
40
|
+
assert.strictEqual(calc.divide(-10, 2), -5);
|
|
41
|
+
assert.strictEqual(calc.divide(1, 3), 1/3);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('should throw error when dividing by zero', () => {
|
|
45
|
+
assert.throws(() => calc.divide(10, 0), {
|
|
46
|
+
name: 'Error',
|
|
47
|
+
message: 'Cannot divide by zero'
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
describe('Advanced Mathematical Operations', () => {
|
|
53
|
+
it('should calculate power correctly', () => {
|
|
54
|
+
assert.strictEqual(calc.power(2, 3), 8);
|
|
55
|
+
assert.strictEqual(calc.power(5, 0), 1);
|
|
56
|
+
assert.strictEqual(calc.power(10, 2), 100);
|
|
57
|
+
assert.strictEqual(calc.power(4, 0.5), 2);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('should calculate square root correctly', () => {
|
|
61
|
+
assert.strictEqual(calc.sqrt(16), 4);
|
|
62
|
+
assert.strictEqual(calc.sqrt(0), 0);
|
|
63
|
+
assert.strictEqual(calc.sqrt(1), 1);
|
|
64
|
+
assert.strictEqual(calc.sqrt(2), Math.sqrt(2));
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('should throw error when calculating square root of negative number', () => {
|
|
68
|
+
assert.throws(() => calc.sqrt(-4), {
|
|
69
|
+
name: 'Error',
|
|
70
|
+
message: 'Cannot calculate square root of negative number'
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('should calculate factorial correctly', () => {
|
|
75
|
+
assert.strictEqual(calc.factorial(5), 120);
|
|
76
|
+
assert.strictEqual(calc.factorial(0), 1);
|
|
77
|
+
assert.strictEqual(calc.factorial(1), 1);
|
|
78
|
+
assert.strictEqual(calc.factorial(3), 6);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('should throw error when calculating factorial of negative number', () => {
|
|
82
|
+
assert.throws(() => calc.factorial(-5), {
|
|
83
|
+
name: 'Error',
|
|
84
|
+
message: 'Cannot calculate factorial of negative number'
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('should calculate percentage correctly', () => {
|
|
89
|
+
assert.strictEqual(calc.percentage(25, 100), 25);
|
|
90
|
+
assert.strictEqual(calc.percentage(10, 50), 20);
|
|
91
|
+
assert.strictEqual(calc.percentage(0, 100), 0);
|
|
92
|
+
assert.strictEqual(calc.percentage(75, 150), 50);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('should throw error when calculating percentage with zero as whole', () => {
|
|
96
|
+
assert.throws(() => calc.percentage(50, 0), {
|
|
97
|
+
name: 'Error',
|
|
98
|
+
message: 'Cannot calculate percentage with zero as whole'
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
describe('Calculation Chain', () => {
|
|
104
|
+
it('should chain calculations correctly', () => {
|
|
105
|
+
const result = calc.chain(10).add(5).multiply(2).subtract(4).divide(2).equals();
|
|
106
|
+
// ((10 + 5) * 2 - 4) / 2 = (30 - 4) / 2 = 26 / 2 = 13
|
|
107
|
+
assert.strictEqual(result, 13);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it('should throw error when dividing by zero in chain', () => {
|
|
111
|
+
assert.throws(() => calc.chain(10).divide(0), {
|
|
112
|
+
name: 'Error',
|
|
113
|
+
message: 'Cannot divide by zero'
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it('should return current value without ending chain', () => {
|
|
118
|
+
const chain = calc.chain(5).add(3);
|
|
119
|
+
assert.strictEqual(chain.value(), 8);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('should support method chaining', () => {
|
|
123
|
+
const chain = calc.chain(2);
|
|
124
|
+
assert.strictEqual(chain.add(3), chain); // Should return itself for chaining
|
|
125
|
+
assert.strictEqual(chain.subtract(1), chain);
|
|
126
|
+
assert.strictEqual(chain.multiply(2), chain);
|
|
127
|
+
assert.strictEqual(chain.divide(2), chain);
|
|
128
|
+
assert.strictEqual(chain.power(2), chain);
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
// Run tests directly if this file is executed
|
|
134
|
+
if (require.main === module) {
|
|
135
|
+
console.log('Running Calculator Unit Tests...');
|
|
136
|
+
|
|
137
|
+
const calc = new Calculator();
|
|
138
|
+
|
|
139
|
+
// Test basic operations
|
|
140
|
+
console.log('\n1. Testing basic arithmetic operations:');
|
|
141
|
+
console.log('calc.add(5, 3) =', calc.add(5, 3));
|
|
142
|
+
console.log('calc.subtract(10, 4) =', calc.subtract(10, 4));
|
|
143
|
+
console.log('calc.multiply(6, 7) =', calc.multiply(6, 7));
|
|
144
|
+
console.log('calc.divide(15, 3) =', calc.divide(15, 3));
|
|
145
|
+
|
|
146
|
+
// Test advanced operations
|
|
147
|
+
console.log('\n2. Testing advanced mathematical operations:');
|
|
148
|
+
console.log('calc.power(2, 3) =', calc.power(2, 3));
|
|
149
|
+
console.log('calc.sqrt(16) =', calc.sqrt(16));
|
|
150
|
+
console.log('calc.factorial(5) =', calc.factorial(5));
|
|
151
|
+
console.log('calc.percentage(25, 100) =', calc.percentage(25, 100) + '%');
|
|
152
|
+
|
|
153
|
+
// Test calculation chain
|
|
154
|
+
console.log('\n3. Testing calculation chain:');
|
|
155
|
+
const chainResult = calc.chain(10).add(5).multiply(2).subtract(4).divide(2).equals();
|
|
156
|
+
console.log('calc.chain(10).add(5).multiply(2).subtract(4).divide(2).equals() =', chainResult);
|
|
157
|
+
|
|
158
|
+
// Test error cases
|
|
159
|
+
console.log('\n4. Testing error handling:');
|
|
160
|
+
try {
|
|
161
|
+
calc.divide(10, 0);
|
|
162
|
+
} catch (error) {
|
|
163
|
+
console.log('Division by zero error:', error.message);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
try {
|
|
167
|
+
calc.sqrt(-4);
|
|
168
|
+
} catch (error) {
|
|
169
|
+
console.log('Square root of negative number error:', error.message);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
try {
|
|
173
|
+
calc.factorial(-5);
|
|
174
|
+
} catch (error) {
|
|
175
|
+
console.log('Factorial of negative number error:', error.message);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
try {
|
|
179
|
+
calc.percentage(50, 0);
|
|
180
|
+
} catch (error) {
|
|
181
|
+
console.log('Percentage with zero as whole error:', error.message);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
try {
|
|
185
|
+
calc.chain(10).divide(0);
|
|
186
|
+
} catch (error) {
|
|
187
|
+
console.log('Chain division by zero error:', error.message);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
console.log('\n✅ All Calculator tests completed!');
|
|
191
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Simple unit tests for Calculator class
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
const Calculator = require('../../src/calculator');
|
|
6
|
+
|
|
7
|
+
console.log("Testing Calculator class...");
|
|
8
|
+
|
|
9
|
+
const calc = new Calculator();
|
|
10
|
+
|
|
11
|
+
try {
|
|
12
|
+
// Test basic operations
|
|
13
|
+
console.log("\n1. Testing basic arithmetic operations:");
|
|
14
|
+
|
|
15
|
+
// Addition
|
|
16
|
+
const sum = calc.add(5, 3);
|
|
17
|
+
console.log(`calc.add(5, 3) = ${sum}`);
|
|
18
|
+
|
|
19
|
+
// Subtraction
|
|
20
|
+
const difference = calc.subtract(10, 4);
|
|
21
|
+
console.log(`calc.subtract(10, 4) = ${difference}`);
|
|
22
|
+
|
|
23
|
+
// Multiplication
|
|
24
|
+
const product = calc.multiply(6, 7);
|
|
25
|
+
console.log(`calc.multiply(6, 7) = ${product}`);
|
|
26
|
+
|
|
27
|
+
// Division
|
|
28
|
+
const quotient = calc.divide(15, 3);
|
|
29
|
+
console.log(`calc.divide(15, 3) = ${quotient}`);
|
|
30
|
+
|
|
31
|
+
// Power
|
|
32
|
+
const powerResult = calc.power(2, 3);
|
|
33
|
+
console.log(`calc.power(2, 3) = ${powerResult}`);
|
|
34
|
+
|
|
35
|
+
// Square root
|
|
36
|
+
const sqrtResult = calc.sqrt(16);
|
|
37
|
+
console.log(`calc.sqrt(16) = ${sqrtResult}`);
|
|
38
|
+
|
|
39
|
+
// Factorial
|
|
40
|
+
const factorialResult = calc.factorial(5);
|
|
41
|
+
console.log(`calc.factorial(5) = ${factorialResult}`);
|
|
42
|
+
|
|
43
|
+
// Percentage
|
|
44
|
+
const percentageResult = calc.percentage(25, 100);
|
|
45
|
+
console.log(`calc.percentage(25, 100) = ${percentageResult}%`);
|
|
46
|
+
|
|
47
|
+
console.log("\n✅ Basic operations tests passed!");
|
|
48
|
+
|
|
49
|
+
// Test calculation chain
|
|
50
|
+
console.log("\n2. Testing calculation chain:");
|
|
51
|
+
const chainResult = calc.chain(10).add(5).multiply(2).subtract(4).divide(2).equals();
|
|
52
|
+
console.log(`calc.chain(10).add(5).multiply(2).subtract(4).divide(2).equals() = ${chainResult}`);
|
|
53
|
+
console.log("✅ Chain operations test passed!");
|
|
54
|
+
|
|
55
|
+
console.log("\n✅ All tests passed!");
|
|
56
|
+
} catch (error) {
|
|
57
|
+
console.error("❌ Test failed:", error.message);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Test error cases
|
|
61
|
+
console.log("\n3. Testing error handling:");
|
|
62
|
+
|
|
63
|
+
try {
|
|
64
|
+
calc.divide(10, 0);
|
|
65
|
+
console.log("❌ Should have thrown an error for division by zero");
|
|
66
|
+
} catch (error) {
|
|
67
|
+
console.log("✅ Correctly threw error for division by zero:", error.message);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
try {
|
|
71
|
+
calc.sqrt(-4);
|
|
72
|
+
console.log("❌ Should have thrown an error for square root of negative number");
|
|
73
|
+
} catch (error) {
|
|
74
|
+
console.log("✅ Correctly threw error for square root of negative number:", error.message);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
try {
|
|
78
|
+
calc.factorial(-5);
|
|
79
|
+
console.log("❌ Should have thrown an error for factorial of negative number");
|
|
80
|
+
} catch (error) {
|
|
81
|
+
console.log("✅ Correctly threw error for factorial of negative number:", error.message);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
try {
|
|
85
|
+
calc.percentage(50, 0);
|
|
86
|
+
console.log("❌ Should have thrown an error for percentage with zero as whole");
|
|
87
|
+
} catch (error) {
|
|
88
|
+
console.log("✅ Correctly threw error for percentage with zero as whole:", error.message);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
try {
|
|
92
|
+
calc.chain(10).divide(0);
|
|
93
|
+
console.log("❌ Should have thrown an error for chain division by zero");
|
|
94
|
+
} catch (error) {
|
|
95
|
+
console.log("✅ Correctly threw error for chain division by zero:", error.message);
|
|
96
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unit tests for Calculator class following project patterns
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
const Calculator = require('../../src/calculator');
|
|
6
|
+
|
|
7
|
+
// Test suite for Calculator
|
|
8
|
+
describe('Calculator', () => {
|
|
9
|
+
let calc;
|
|
10
|
+
|
|
11
|
+
beforeEach(() => {
|
|
12
|
+
calc = new Calculator();
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
describe('Basic Operations', () => {
|
|
16
|
+
test('should add two numbers correctly', () => {
|
|
17
|
+
expect(calc.add(5, 3)).toBe(8);
|
|
18
|
+
expect(calc.add(-2, 7)).toBe(5);
|
|
19
|
+
expect(calc.add(0, 0)).toBe(0);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test('should subtract two numbers correctly', () => {
|
|
23
|
+
expect(calc.subtract(10, 4)).toBe(6);
|
|
24
|
+
expect(calc.subtract(5, 8)).toBe(-3);
|
|
25
|
+
expect(calc.subtract(0, 0)).toBe(0);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test('should multiply two numbers correctly', () => {
|
|
29
|
+
expect(calc.multiply(6, 7)).toBe(42);
|
|
30
|
+
expect(calc.multiply(-3, 4)).toBe(-12);
|
|
31
|
+
expect(calc.multiply(0, 100)).toBe(0);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test('should divide two numbers correctly', () => {
|
|
35
|
+
expect(calc.divide(15, 3)).toBe(5);
|
|
36
|
+
expect(calc.divide(7, 2)).toBe(3.5);
|
|
37
|
+
expect(calc.divide(-10, 2)).toBe(-5);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test('should throw error when dividing by zero', () => {
|
|
41
|
+
expect(() => calc.divide(10, 0)).toThrow('Cannot divide by zero');
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test('should calculate power correctly', () => {
|
|
45
|
+
expect(calc.power(2, 3)).toBe(8);
|
|
46
|
+
expect(calc.power(5, 0)).toBe(1);
|
|
47
|
+
expect(calc.power(10, 2)).toBe(100);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test('should calculate square root correctly', () => {
|
|
51
|
+
expect(calc.sqrt(16)).toBe(4);
|
|
52
|
+
expect(calc.sqrt(0)).toBe(0);
|
|
53
|
+
expect(calc.sqrt(1)).toBe(1);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test('should throw error when calculating square root of negative number', () => {
|
|
57
|
+
expect(() => calc.sqrt(-4)).toThrow('Cannot calculate square root of negative number');
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test('should calculate factorial correctly', () => {
|
|
61
|
+
expect(calc.factorial(5)).toBe(120);
|
|
62
|
+
expect(calc.factorial(0)).toBe(1);
|
|
63
|
+
expect(calc.factorial(1)).toBe(1);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test('should throw error when calculating factorial of negative number', () => {
|
|
67
|
+
expect(() => calc.factorial(-5)).toThrow('Cannot calculate factorial of negative number');
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test('should calculate percentage correctly', () => {
|
|
71
|
+
expect(calc.percentage(25, 100)).toBe(25);
|
|
72
|
+
expect(calc.percentage(10, 50)).toBe(20);
|
|
73
|
+
expect(calc.percentage(0, 100)).toBe(0);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test('should throw error when calculating percentage with zero as whole', () => {
|
|
77
|
+
expect(() => calc.percentage(50, 0)).toThrow('Cannot calculate percentage with zero as whole');
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
describe('Calculation Chain', () => {
|
|
82
|
+
test('should chain calculations correctly', () => {
|
|
83
|
+
const result = calc.chain(10).add(5).multiply(2).subtract(4).divide(2).equals();
|
|
84
|
+
// ((10 + 5) * 2 - 4) / 2 = (30 - 4) / 2 = 26 / 2 = 13
|
|
85
|
+
expect(result).toBe(13);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test('should throw error when dividing by zero in chain', () => {
|
|
89
|
+
expect(() => calc.chain(10).divide(0)).toThrow('Cannot divide by zero');
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
test('should return current value without ending chain', () => {
|
|
93
|
+
const chain = calc.chain(5).add(3);
|
|
94
|
+
expect(chain.value()).toBe(8);
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
});
|