summaryrefslogtreecommitdiff
path: root/internal/perc/perc_test.go
blob: f8d6f97a457540d48ad36ed17f812ca1d464d724 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 Paul Buetow

package perc

import (
	"strings"
	"testing"
)

// commonTestCases contains common test case patterns used across multiple tests
var commonTestCases = []struct {
	name     string
	input    string
	expected string
}{
	{
		name:     "20% of 150",
		input:    "20% of 150",
		expected: "20.00% of 150.00 = 30.00",
	},
	{
		name:     "what is 20% of 150",
		input:    "what is 20% of 150",
		expected: "20.00% of 150.00 = 30.00",
	},
	{
		name:     "50% of 200",
		input:    "50% of 200",
		expected: "50.00% of 200.00 = 100.00",
	},
	{
		name:     "decimal percent",
		input:    "12.5% of 80",
		expected: "12.50% of 80.00 = 10.00",
	},
	{
		name:     "decimal base",
		input:    "20% of 75.5",
		expected: "20.00% of 75.50 = 15.10",
	},
	{
		name:     "without 'of'",
		input:    "25% 400",
		expected: "25.00% of 400.00 = 100.00",
	},
	{
		name:     "30 is what % of 150",
		input:    "30 is what % of 150",
		expected: "30.00 is 20.00% of 150.00",
	},
	{
		name:     "50 is what % of 200",
		input:    "50 is what % of 200",
		expected: "50.00 is 25.00% of 200.00",
	},
	{
		name:     "decimal values",
		input:    "12.5 is what % of 50",
		expected: "12.50 is 25.00% of 50.00",
	},
	{
		name:     "without spaces around %",
		input:    "75 is what% of 300",
		expected: "75.00 is 25.00% of 300.00",
	},
	{
		name:     "without 'of'",
		input:    "100 is what % 400",
		expected: "100.00 is 25.00% of 400.00",
	},
	{
		name:     "30 is 20% of what",
		input:    "30 is 20% of what",
		expected: "30.00 is 20.00% of 150.00",
	},
	{
		name:     "50 is 25% of what",
		input:    "50 is 25% of what",
		expected: "50.00 is 25.00% of 200.00",
	},
	{
		name:     "decimal values",
		input:    "15 is 30% of what",
		expected: "15.00 is 30.00% of 50.00",
	},
	{
		name:     "without spaces around %",
		input:    "75 is 25% of what",
		expected: "75.00 is 25.00% of 300.00",
	},
	{
		name:     "without 'of'",
		input:    "40 is 20% what",
		expected: "40.00 is 20.00% of 200.00",
	},
}

// runParseTest runs a parse test with common validation logic
func runParseTest(t *testing.T, tests []struct {
	name     string
	input    string
	expected string
}) {
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			result, err := Parse(tt.input)
			if err != nil {
				t.Fatalf("Parse(%q) returned error: %v", tt.input, err)
			}
			if !strings.HasPrefix(result, tt.expected) {
				t.Errorf("Parse(%q) = %q, expected to start with %q", tt.input, result, tt.expected)
			}
			if !strings.Contains(result, "Steps:") {
				t.Errorf("Parse(%q) = %q, expected to contain calculation steps", tt.input, result)
			}
		})
	}
}
func TestParseXPercentOfY(t *testing.T) {
	tests := []struct {
		name     string
		input    string
		expected string
	}{
		commonTestCases[0], // "20% of 150"
		commonTestCases[1], // "what is 20% of 150"
		commonTestCases[2], // "50% of 200"
		commonTestCases[3], // "decimal percent"
		commonTestCases[4], // "decimal base"
		commonTestCases[5], // "without 'of'"
	}
	runParseTest(t, tests)
}

func TestParseXIsWhatPercentOfY(t *testing.T) {
	tests := []struct {
		name     string
		input    string
		expected string
	}{
		commonTestCases[6],  // "30 is what % of 150"
		commonTestCases[7],  // "50 is what % of 200"
		commonTestCases[8],  // "decimal values"
		commonTestCases[9],  // "without spaces around %"
		commonTestCases[10], // "without 'of'"
	}
	runParseTest(t, tests)
}

func TestParseXIsYPercentOfWhat(t *testing.T) {
	tests := []struct {
		name     string
		input    string
		expected string
	}{
		commonTestCases[11], // "30 is 20% of what"
		commonTestCases[12], // "50 is 25% of what"
		commonTestCases[13], // "decimal values"
		commonTestCases[14], // "without spaces around %"
		commonTestCases[15], // "without 'of'"
	}
	runParseTest(t, tests)
}

func TestParseErrors(t *testing.T) {
	tests := []struct {
		name  string
		input string
	}{
		{
			name:  "invalid input",
			input: "hello world",
		},
		{
			name:  "incomplete input",
			input: "20%",
		},
		{
			name:  "missing numbers",
			input: "% of",
		},
		{
			name:  "random text",
			input: "calculate percentage",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			_, err := Parse(tt.input)
			if err == nil {
				t.Errorf("Parse(%q) expected error, got nil", tt.input)
			}
		})
	}
}

func TestParseCaseInsensitive(t *testing.T) {
	tests := []struct {
		name  string
		input string
	}{
		{
			name:  "uppercase WHAT IS",
			input: "WHAT IS 20% OF 150",
		},
		{
			name:  "mixed case What Is",
			input: "What Is 20% Of 150",
		},
		{
			name:  "uppercase IS WHAT",
			input: "30 IS WHAT % OF 150",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			_, err := Parse(tt.input)
			if err != nil {
				t.Errorf("Parse(%q) should be case-insensitive, got error: %v", tt.input, err)
			}
		})
	}
}

func TestParseDivisionByZero(t *testing.T) {
	tests := []struct {
		name  string
		input string
	}{
		{
			name:  "X is what % of 0",
			input: "30 is what % of 0",
		},
		{
			name:  "X is 0% of what",
			input: "30 is 0% of what",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			_, err := Parse(tt.input)
			if err == nil {
				t.Errorf("Parse(%q) should handle division by zero, expected error", tt.input)
			}
		})
	}
}

func TestParseWhitespace(t *testing.T) {
	tests := []struct {
		name     string
		input    string
		expected string
	}{
		{
			name:     "extra spaces",
			input:    "  20%   of   150  ",
			expected: "20.00% of 150.00 = 30.00",
		},
		{
			name:     "tabs and spaces",
			input:    "30  is  what  %  of  150",
			expected: "30.00 is 20.00% of 150.00",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			result, err := Parse(tt.input)
			if err != nil {
				t.Fatalf("Parse(%q) returned error: %v", tt.input, err)
			}
			if !strings.Contains(result, "of") {
				t.Errorf("Parse(%q) should handle whitespace properly, got %q", tt.input, result)
			}
		})
	}
}

// TestParseCalculationPercentOfY tests ParseCalculation for "X% of Y" type
func TestParseCalculationPercentOfY(t *testing.T) {
	calc, err := ParseCalculation("20% of 150")
	if err != nil {
		t.Fatalf("ParseCalculation returned error: %v", err)
	}
	if calc == nil {
		t.Fatal("ParseCalculation returned nil")
	}
	if calc.Type != PercentOfY {
		t.Errorf("Type = %v, want PercentOfY", calc.Type)
	}
	if calc.Percent != 20 {
		t.Errorf("Percent = %v, want 20", calc.Percent)
	}
	if calc.Base != 150 {
		t.Errorf("Base = %v, want 150", calc.Base)
	}
	if calc.Result != 30 {
		t.Errorf("Result = %v, want 30", calc.Result)
	}
	if calc.Steps == "" {
		t.Error("Steps should not be empty")
	}
}

// TestParseCalculationIsWhatPercentOfY tests ParseCalculation for "X is what % of Y" type
func TestParseCalculationIsWhatPercentOfY(t *testing.T) {
	calc, err := ParseCalculation("30 is what % of 150")
	if err != nil {
		t.Fatalf("ParseCalculation returned error: %v", err)
	}
	if calc == nil {
		t.Fatal("ParseCalculation returned nil")
	}
	if calc.Type != IsWhatPercentOfY {
		t.Errorf("Type = %v, want IsWhatPercentOfY", calc.Type)
	}
	if calc.Percent != 20 {
		t.Errorf("Percent = %v, want 20", calc.Percent)
	}
	if calc.Base != 150 {
		t.Errorf("Base = %v, want 150", calc.Base)
	}
	if calc.Result != 30 {
		t.Errorf("Result = %v, want 30", calc.Result)
	}
}

// TestParseCalculationIsYPercentOfWhat tests ParseCalculation for "X is Y% of what" type
func TestParseCalculationIsYPercentOfWhat(t *testing.T) {
	calc, err := ParseCalculation("30 is 20% of what")
	if err != nil {
		t.Fatalf("ParseCalculation returned error: %v", err)
	}
	if calc == nil {
		t.Fatal("ParseCalculation returned nil")
	}
	if calc.Type != IsYPercentOfWhat {
		t.Errorf("Type = %v, want IsYPercentOfWhat", calc.Type)
	}
	if calc.Percent != 20 {
		t.Errorf("Percent = %v, want 20", calc.Percent)
	}
	if calc.Base != 150 {
		t.Errorf("Base = %v, want 150", calc.Base)
	}
	if calc.Result != 30 {
		t.Errorf("Result = %v, want 30", calc.Result)
	}
}

// TestParseCalculationWhatIsPrefix tests "what is" prefix stripping
func TestParseCalculationWhatIsPrefix(t *testing.T) {
	calc, err := ParseCalculation("what is 20% of 150")
	if err != nil {
		t.Fatalf("ParseCalculation returned error: %v", err)
	}
	if calc.Percent != 20 || calc.Base != 150 || calc.Result != 30 {
		t.Errorf("ParseCalculation with 'what is' prefix: Percent=%v, Base=%v, Result=%v",
			calc.Percent, calc.Base, calc.Result)
	}
}

// TestParseCalculationCaseInsensitive tests case insensitivity
func TestParseCalculationCaseInsensitive(t *testing.T) {
	tests := []struct {
		input    string
		wantType CalculationType
	}{
		{"20% OF 150", PercentOfY},
		{"WHAT IS 20% OF 150", PercentOfY},
		{"30 IS WHAT % OF 150", IsWhatPercentOfY},
		{"30 IS 20% OF WHAT", IsYPercentOfWhat},
	}

	for _, tt := range tests {
		t.Run(tt.input, func(t *testing.T) {
			calc, err := ParseCalculation(tt.input)
			if err != nil {
				t.Fatalf("ParseCalculation(%q) returned error: %v", tt.input, err)
			}
			if calc.Type != tt.wantType {
				t.Errorf("Type = %v, want %v", calc.Type, tt.wantType)
			}
		})
	}
}

// TestParseCalculationDivisionByZero tests division by zero edge cases
func TestParseCalculationDivisionByZero(t *testing.T) {
	tests := []struct {
		name  string
		input string
	}{
		{"X is what % of 0", "30 is what % of 0"},
		{"X is 0% of what", "30 is 0% of what"},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			_, err := ParseCalculation(tt.input)
			if err == nil {
				t.Errorf("ParseCalculation(%q) should return error for division by zero", tt.input)
			}
		})
	}
}

// TestParseCalculationInvalidInput tests invalid inputs return errors
func TestParseCalculationInvalidInput(t *testing.T) {
	tests := []string{
		"hello world",
		"20%",
		"% of",
		"calculate percentage",
		"",
	}

	for _, input := range tests {
		t.Run(input, func(t *testing.T) {
			_, err := ParseCalculation(input)
			if err == nil {
				t.Errorf("ParseCalculation(%q) should return error", input)
			}
		})
	}
}

// TestCalculationFormatPercentOfY tests Format() for PercentOfY type
func TestCalculationFormatPercentOfY(t *testing.T) {
	calc := &Calculation{
		Type:    PercentOfY,
		Percent: 20,
		Base:    150,
		Result:  30,
		Steps:   "(20 / 100) * 150 = 0.20 * 150 = 30",
	}

	formatted := calc.Format()
	if !strings.HasPrefix(formatted, "20.00% of 150.00 = 30.00") {
		t.Errorf("Format() = %q, expected to start with '20.00%% of 150.00 = 30.00'", formatted)
	}
	if !strings.Contains(formatted, "Steps:") {
		t.Error("Format() should contain Steps")
	}
}

// TestCalculationFormatIsWhatPercentOfY tests Format() for IsWhatPercentOfY type
func TestCalculationFormatIsWhatPercentOfY(t *testing.T) {
	calc := &Calculation{
		Type:    IsWhatPercentOfY,
		Percent: 20,
		Base:    150,
		Result:  30,
		Steps:   "",
	}

	formatted := calc.Format()
	if !strings.HasPrefix(formatted, "30.00 is 20.00% of 150.00") {
		t.Errorf("Format() = %q, expected to start with '30.00 is 20.00%% of 150.00'", formatted)
	}
}

// TestCalculationFormatIsYPercentOfWhat tests Format() for IsYPercentOfWhat type
func TestCalculationFormatIsYPercentOfWhat(t *testing.T) {
	calc := &Calculation{
		Type:    IsYPercentOfWhat,
		Percent: 20,
		Base:    150,
		Result:  30,
		Steps:   "",
	}

	formatted := calc.Format()
	if !strings.HasPrefix(formatted, "30.00 is 20.00% of 150.00") {
		t.Errorf("Format() = %q, expected to start with '30.00 is 20.00%% of 150.00'", formatted)
	}
}

// TestCalculationFormatWithoutSteps tests Format() without Steps field
func TestCalculationFormatWithoutSteps(t *testing.T) {
	calc := &Calculation{
		Type:    PercentOfY,
		Percent: 25,
		Base:    100,
		Result:  25,
		Steps:   "",
	}

	formatted := calc.Format()
	if formatted != "25.00% of 100.00 = 25.00" {
		t.Errorf("Format() without steps = %q, want '25.00%% of 100.00 = 25.00'", formatted)
	}
	if strings.Contains(formatted, "Steps") {
		t.Error("Format() should not contain 'Steps' when Steps is empty")
	}
}

// TestParseCalculationDecimalValues tests decimal values in ParseCalculation
func TestParseCalculationDecimalValues(t *testing.T) {
	calc, err := ParseCalculation("12.5% of 80")
	if err != nil {
		t.Fatalf("ParseCalculation returned error: %v", err)
	}
	if calc.Percent != 12.5 {
		t.Errorf("Percent = %v, want 12.5", calc.Percent)
	}
	if calc.Result != 10 {
		t.Errorf("Result = %v, want 10", calc.Result)
	}
}

// TestParseCalculationLargeValues tests large values in ParseCalculation
func TestParseCalculationLargeValues(t *testing.T) {
	calc, err := ParseCalculation("50% of 1000000")
	if err != nil {
		t.Fatalf("ParseCalculation returned error: %v", err)
	}
	if calc.Percent != 50 {
		t.Errorf("Percent = %v, want 50", calc.Percent)
	}
	if calc.Result != 500000 {
		t.Errorf("Result = %v, want 500000", calc.Result)
	}
}