| OLD | NEW |
| 1 /// <reference path="../typings/mocha/mocha.d.ts"/> | 1 /// <reference path="../typings/mocha/mocha.d.ts"/> |
| 2 import {expectTranslate, expectErroneousCode} from './test_support'; | 2 import {expectTranslate} from './test_support'; |
| 3 | 3 |
| 4 function expectTranslates(cases: any) { | 4 function expectEmptyTranslates(cases: string[]) { |
| 5 for (let tsCode of Object.keys(cases)) { | 5 for (let tsCode of cases) { |
| 6 expectTranslate(tsCode).to.equal(cases[tsCode]); | 6 expectTranslate(tsCode).to.equal(''); |
| 7 } | 7 } |
| 8 } | 8 } |
| 9 | 9 |
| 10 // TODO(jacobr): we don't really need to be specifying separate code for the | 10 // TODO(jacobr): we don't really need to be specifying separate code for the |
| 11 // JS and Dart version for these tests as the code formatting is identical. | 11 // JS and Dart version for these tests as the code formatting is identical. |
| 12 describe('expressions', () => { | 12 describe('ignore expressions', () => { |
| 13 it('does math', () => { | 13 it('math', () => { |
| 14 expectTranslates({ | 14 expectEmptyTranslates([ |
| 15 '1 + 2': '1 + 2;', | 15 '1 + 2', |
| 16 '1 - 2': '1 - 2;', | 16 '1 - 2', |
| 17 '1 * 2': '1 * 2;', | 17 '1 * 2', |
| 18 '1 / 2': '1 / 2;', | 18 '1 / 2', |
| 19 '1 % 2': '1 % 2;', | 19 '1 % 2', |
| 20 'x++': 'x++;', | 20 'x++', |
| 21 'x--': 'x--;', | 21 'x--', |
| 22 '++x': '++x;', | 22 '++x', |
| 23 '--x': '--x;', | 23 '--x', |
| 24 '-x': '-x;', | 24 '-x', |
| 25 }); | 25 ]); |
| 26 }); | 26 }); |
| 27 it('assigns', () => { | 27 it('assigns', () => { |
| 28 expectTranslates({ | 28 expectEmptyTranslates([ |
| 29 'x += 1': 'x += 1;', | 29 'x += 1', |
| 30 'x -= 1': 'x -= 1;', | 30 'x -= 1', |
| 31 'x *= 1': 'x *= 1;', | 31 'x *= 1', |
| 32 'x /= 1': 'x /= 1;', | 32 'x /= 1', |
| 33 'x %= 1': 'x %= 1;', | 33 'x %= 1', |
| 34 'x <<= 1': 'x = (x as int) << 1;', | 34 'x <<= 1', |
| 35 'x >>= 1': 'x = (x as int) >> 1;', | 35 'x >>= 1', |
| 36 // 'x >>>= 1': 'x >>>= 1;', // This doesn't appear to be a valid oper
ator in Dart. | 36 'x >>>= 1', |
| 37 'x &= 1': 'x = (x as int) & 1;', | 37 'x &= 1', |
| 38 'x ^= 1': 'x = (x as int) ^ 1;', | 38 'x ^= 1', |
| 39 'x |= 1': 'x = (x as int) | 1;', | 39 'x |= 1', |
| 40 }); | 40 ]); |
| 41 }); | 41 }); |
| 42 it('compares', () => { | 42 it('compares', () => { |
| 43 expectTranslates({ | 43 expectEmptyTranslates([ |
| 44 '1 == 2': '1 == 2;', | 44 '1 == 2', |
| 45 '1 != 2': '1 != 2;', | 45 '1 != 2', |
| 46 '1 > 2': '1 > 2;', | 46 '1 > 2', |
| 47 '1 < 2': '1 < 2;', | 47 '1 < 2', |
| 48 '1 >= 2': '1 >= 2;', | 48 '1 >= 2', |
| 49 '1 <= 2': '1 <= 2;', | 49 '1 <= 2', |
| 50 }); | 50 ]); |
| 51 }); | 51 }); |
| 52 it('compares identity', () => { | 52 it('compares identity', () => { expectEmptyTranslates(['1 === 2', '1 !== 2']);
}); |
| 53 expectTranslate('1 === 2').to.equal('identical(1, 2);'); | |
| 54 expectTranslate('1 !== 2').to.equal('!identical(1, 2);'); | |
| 55 }); | |
| 56 it('bit fiddles', () => { | 53 it('bit fiddles', () => { |
| 57 expectTranslates({ | 54 expectEmptyTranslates( |
| 58 'x & 2': '(x as int) & 2;', | 55 ['x & 2', '1 & 2', '1 | 2', '1 ^ 2', '~1', '1 << 2', '1 >> 2', '0x1 & 0x
2', '1 >>> 2']); |
| 59 '1 & 2': '1 & 2;', | |
| 60 '1 | 2': '1 | 2;', | |
| 61 '1 ^ 2': '1 ^ 2;', | |
| 62 '~1': '~1;', | |
| 63 '1 << 2': '1 << 2;', | |
| 64 '1 >> 2': '1 >> 2;', | |
| 65 '0x1 & 0x2': '0x1 & 0x2;', | |
| 66 // '1 >>> 2': '1 >>> 2;', // This doesn't appear to be a valid opera
tor in Dart. | |
| 67 }); | |
| 68 }); | 56 }); |
| 69 it('translates logic', () => { | 57 it('translates logic', () => { |
| 70 expectTranslates({ | 58 expectEmptyTranslates([ |
| 71 '1 && 2': '1 && 2;', | 59 '1 && 2', |
| 72 '1 || 2': '1 || 2;', | 60 '1 || 2', |
| 73 '!1': '!1;', | 61 '!1', |
| 74 }); | 62 ]); |
| 75 }); | 63 }); |
| 76 it('translates ternary', | 64 it('translates ternary', () => { |
| 77 () => { expectTranslate('var x = 1 ? 2 : 3').to.equal('var x = 1 ? 2 : 3;')
; }); | 65 expectTranslate('var x = 1 ? 2 : 3').to.equal(`@JS() |
| 78 it('translates the comma operator', | 66 external get x; |
| 79 () => { expectTranslate('var x = [1, 2]').to.equal('var x = [1, 2];'); }); | 67 @JS() |
| 80 it('translates "in"', | 68 external set x(v);`); |
| 81 () => { expectErroneousCode('x in y').to.throw('in operator is unsupported'
); }); | 69 }); |
| 82 it('translates "instanceof"', | 70 it('translates the comma operator', () => { |
| 83 () => { expectTranslate('1 instanceof Foo').to.equal('1 is Foo;'); }); | 71 expectTranslate('var x = [1, 2]').to.equal(`@JS() |
| 84 it('translates "this"', () => { expectTranslate('this.x').to.equal('this.x;');
}); | 72 external get x; |
| 85 it('translates "delete"', | 73 @JS() |
| 86 () => { expectErroneousCode('delete x[y];').to.throw('delete operator is un
supported'); }); | 74 external set x(v);`); |
| 87 it('translates "typeof"', | 75 }); |
| 88 () => { expectErroneousCode('typeof x;').to.throw('typeof operator is unsup
ported'); }); | 76 it('translates "in"', () => { expectTranslate('x in y').to.equal(''); }); |
| 89 it('translates "void"', | 77 it('translates "instanceof"', () => { expectTranslate('1 instanceof Foo').to.e
qual(''); }); |
| 90 () => { expectErroneousCode('void x;').to.throw('void operator is unsupport
ed'); }); | 78 it('translates "this"', () => { expectTranslate('this.x').to.equal(''); }); |
| 91 it('translates parens', () => { expectTranslate('(1)').to.equal('(1);'); }); | 79 it('translates "delete"', () => { expectTranslate('delete x[y];').to.equal('')
; }); |
| 80 it('translates "typeof"', () => { expectTranslate('typeof x;').to.equal(''); }
); |
| 81 it('translates "void"', () => { expectTranslate('void x;').to.equal(''); }); |
| 82 it('translates parens', () => { expectTranslate('(1)').to.equal(''); }); |
| 92 | 83 |
| 93 it('translates property paths', () => { | 84 it('translates property paths', () => { |
| 94 expectTranslate('foo.bar;').to.equal('foo.bar;'); | 85 expectTranslate('foo.bar;').to.equal(''); |
| 95 expectTranslate('foo[bar];').to.equal('foo[bar];'); | 86 expectTranslate('foo[bar];').to.equal(''); |
| 96 }); | 87 }); |
| 97 }); | 88 }); |
| OLD | NEW |