|
| 1 | +import { TestBed } from '@angular/core/testing'; |
| 2 | +import { InterpolationPipe } from './interpolation.pipe'; |
| 3 | +import { provideTranslation } from './translation.provider'; |
| 4 | + |
| 5 | +describe('InterpolationPipe', () => { |
| 6 | + let pipe: InterpolationPipe; |
| 7 | + |
| 8 | + beforeEach(() => { |
| 9 | + TestBed.configureTestingModule({ |
| 10 | + providers: [InterpolationPipe, provideTranslation()], |
| 11 | + }); |
| 12 | + pipe = TestBed.inject(InterpolationPipe); |
| 13 | + }); |
| 14 | + |
| 15 | + it('should create an instance', () => { |
| 16 | + expect(pipe).toBeTruthy(); |
| 17 | + }); |
| 18 | + |
| 19 | + describe('Simple Interpolation', () => { |
| 20 | + it('should handle basic variable replacement', () => { |
| 21 | + expect(pipe.transform('Hello, {{name}}!', { name: 'World' })).toBe('Hello, World!'); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should handle multiple variables', () => { |
| 25 | + expect(pipe.transform('{{greeting}}, {{name}}!', { greeting: 'Hello', name: 'World' })).toBe('Hello, World!'); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should ignore spaces in variable names', () => { |
| 29 | + expect(pipe.transform('Hello, {{ name }}!', { name: 'World' })).toBe('Hello, World!'); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should leave unmatched variables unchanged', () => { |
| 33 | + expect(pipe.transform('Hello, {{name}}! {{missing}}', { name: 'World' })).toBe('Hello, World! {{missing}}'); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should handle number variables', () => { |
| 37 | + expect(pipe.transform('You have {{count}} messages.', { count: 5 })).toBe('You have 5 messages.'); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should handle boolean variables', () => { |
| 41 | + expect(pipe.transform('Notifications are {{enabled}}.', { enabled: true })).toBe('Notifications are true.'); |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + describe('Select Rules', () => { |
| 46 | + it('should handle basic select rule', () => { |
| 47 | + const input = 'mySelectRule: "{gender, select, male {He} female {She} other {They}}"'; |
| 48 | + expect(pipe.transform(input, { gender: 'male' })).toBe('mySelectRule: "He"'); |
| 49 | + expect(pipe.transform(input, { gender: 'female' })).toBe('mySelectRule: "She"'); |
| 50 | + expect(pipe.transform(input, { gender: 'other' })).toBe('mySelectRule: "They"'); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should use "other" as fallback in select rule', () => { |
| 54 | + const input = 'mySelectRule: "{gender, select, male {He} female {She} other {They}}"'; |
| 55 | + expect(pipe.transform(input, { gender: 'unknown' })).toBe('mySelectRule: "They"'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should handle select rule with complex values', () => { |
| 59 | + const input = 'mySelectRule: "{user, select, admin {Full access} moderator {Limited access} other {No access}}"'; |
| 60 | + expect(pipe.transform(input, { user: 'admin' })).toBe('mySelectRule: "Full access"'); |
| 61 | + expect(pipe.transform(input, { user: 'moderator' })).toBe('mySelectRule: "Limited access"'); |
| 62 | + expect(pipe.transform(input, { user: 'guest' })).toBe('mySelectRule: "No access"'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should handle multiple select rules', () => { |
| 66 | + const input = |
| 67 | + 'gender: "{gender, select, male {He} female {She} other {They}}" pronoun: "{gender, select, male {his} female {her} other {their}}"'; |
| 68 | + expect(pipe.transform(input, { gender: 'male' })).toBe('gender: "He" pronoun: "his"'); |
| 69 | + expect(pipe.transform(input, { gender: 'female' })).toBe('gender: "She" pronoun: "her"'); |
| 70 | + expect(pipe.transform(input, { gender: 'other' })).toBe('gender: "They" pronoun: "their"'); |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + describe('Plural Rules', () => { |
| 75 | + it('should handle basic plural rule', () => { |
| 76 | + const input = 'myPluralRule: "{count, plural, =0 {no results} one {1 result} other {# results}}"'; |
| 77 | + expect(pipe.transform(input, { count: 0 })).toBe('myPluralRule: "no results"'); |
| 78 | + expect(pipe.transform(input, { count: 1 })).toBe('myPluralRule: "1 result"'); |
| 79 | + expect(pipe.transform(input, { count: 2 })).toBe('myPluralRule: "2 results"'); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should handle exact matches in plural rules', () => { |
| 83 | + const input = 'myPluralRule: "{count, plural, =0 {no results} =1 {exactly one result} other {# results}}"'; |
| 84 | + expect(pipe.transform(input, { count: 0 })).toBe('myPluralRule: "no results"'); |
| 85 | + expect(pipe.transform(input, { count: 1 })).toBe('myPluralRule: "exactly one result"'); |
| 86 | + expect(pipe.transform(input, { count: 2 })).toBe('myPluralRule: "2 results"'); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should replace # with the actual count', () => { |
| 90 | + const input = 'myPluralRule: "{count, plural, =0 {no results} one {# result} other {# results}}"'; |
| 91 | + expect(pipe.transform(input, { count: 5 })).toBe('myPluralRule: "5 results"'); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should handle multiple plural rules', () => { |
| 95 | + const input = |
| 96 | + 'apples: "{apples, plural, =0 {no apples} one {1 apple} other {# apples}}" oranges: "{oranges, plural, =0 {no oranges} one {1 orange} other {# oranges}}"'; |
| 97 | + expect(pipe.transform(input, { apples: 0, oranges: 1 })).toBe('apples: "no apples" oranges: "1 orange"'); |
| 98 | + expect(pipe.transform(input, { apples: 1, oranges: 2 })).toBe('apples: "1 apple" oranges: "2 oranges"'); |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + describe('Complex Scenarios', () => { |
| 103 | + it('should handle nested interpolation, select, and plural rules', () => { |
| 104 | + const input = |
| 105 | + '{{user}} has myPluralRule: "{count, plural, =0 {no {{item}}s} one {1 {{item}}} other {# {{item}}s}}" and mySelectRule: "{gender, select, male {he likes} female {she likes} other {they like}} it."'; |
| 106 | + const result = pipe.transform(input, { user: 'Alice', count: 2, item: 'apple', gender: 'female' }); |
| 107 | + expect(result).toBe('Alice has myPluralRule: "2 apples" and mySelectRule: "she likes it."'); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should handle multiple nested rules', () => { |
| 111 | + const input = |
| 112 | + 'gender: "{gender, select, male {He has} female {She has} other {They have}}" count: "{count, plural, =0 {no items} one {one item} other {# items}}"'; |
| 113 | + expect(pipe.transform(input, { gender: 'male', count: 0 })).toBe('gender: "He has" count: "no items"'); |
| 114 | + expect(pipe.transform(input, { gender: 'female', count: 1 })).toBe('gender: "She has" count: "one item"'); |
| 115 | + expect(pipe.transform(input, { gender: 'other', count: 5 })).toBe('gender: "They have" count: "5 items"'); |
| 116 | + }); |
| 117 | + }); |
| 118 | + |
| 119 | + describe('Edge Cases', () => { |
| 120 | + it('should return an empty string for null input', () => { |
| 121 | + expect(pipe.transform(null, {})).toBe(''); |
| 122 | + }); |
| 123 | + |
| 124 | + it('should return an empty string for undefined input', () => { |
| 125 | + expect(pipe.transform(undefined, {})).toBe(''); |
| 126 | + }); |
| 127 | + |
| 128 | + it('should handle missing arguments', () => { |
| 129 | + const input = '{{var1}} {{var2}} mySelectRule: "{var3, select, a {A} b {B} other {C}}"'; |
| 130 | + expect(pipe.transform(input, { var1: 'Hello' })).toBe('Hello {{var2}} mySelectRule: "C"'); |
| 131 | + }); |
| 132 | + |
| 133 | + it('should handle empty select and plural rules', () => { |
| 134 | + const input = 'select: "{var, select,}" plural: "{count, plural,}"'; |
| 135 | + expect(pipe.transform(input, { var: 'test', count: 5 })).toBe('select: "" plural: ""'); |
| 136 | + }); |
| 137 | + }); |
| 138 | +}); |
0 commit comments