|
| 1 | +import RubyGenerator from '../../../../src/lib/ruby-generator'; |
| 2 | +import LooksBlocks from '../../../../src/lib/ruby-generator/looks'; |
| 3 | + |
| 4 | +describe('RubyGenerator/Looks', () => { |
| 5 | + beforeEach(() => { |
| 6 | + RubyGenerator.cache_ = { |
| 7 | + comments: {}, |
| 8 | + targetCommentTexts: [] |
| 9 | + }; |
| 10 | + RubyGenerator.definitions_ = {}; |
| 11 | + RubyGenerator.functionNames_ = {}; |
| 12 | + RubyGenerator.currentTarget = null; |
| 13 | + LooksBlocks(RubyGenerator); |
| 14 | + }); |
| 15 | + |
| 16 | + describe('looks_say', () => { |
| 17 | + test('normal say', () => { |
| 18 | + const block = { |
| 19 | + id: 'block-id', |
| 20 | + opcode: 'looks_say', |
| 21 | + inputs: { |
| 22 | + MESSAGE: {} |
| 23 | + } |
| 24 | + }; |
| 25 | + RubyGenerator.valueToCode = jest.fn().mockReturnValue('"Hello!"'); |
| 26 | + const expected = 'say("Hello!")\n'; |
| 27 | + expect(RubyGenerator.looks_say(block)).toEqual(expected); |
| 28 | + }); |
| 29 | + |
| 30 | + test('with @ruby:method:print', () => { |
| 31 | + const block = { |
| 32 | + id: 'block-id', |
| 33 | + opcode: 'looks_say', |
| 34 | + inputs: { |
| 35 | + MESSAGE: {} |
| 36 | + } |
| 37 | + }; |
| 38 | + RubyGenerator.cache_.comments['block-id'] = { text: '@ruby:method:print' }; |
| 39 | + RubyGenerator.valueToCode = jest.fn().mockReturnValue('"Hello!"'); |
| 40 | + const expected = 'print("Hello!")\n'; |
| 41 | + expect(RubyGenerator.looks_say(block)).toEqual(expected); |
| 42 | + }); |
| 43 | + |
| 44 | + test('with @ruby:method:puts', () => { |
| 45 | + const block = { |
| 46 | + id: 'block-id', |
| 47 | + opcode: 'looks_say', |
| 48 | + inputs: { |
| 49 | + MESSAGE: {} |
| 50 | + } |
| 51 | + }; |
| 52 | + RubyGenerator.cache_.comments['block-id'] = { text: '@ruby:method:puts' }; |
| 53 | + RubyGenerator.valueToCode = jest.fn().mockReturnValue('"Hello!"'); |
| 54 | + const expected = 'puts("Hello!")\n'; |
| 55 | + expect(RubyGenerator.looks_say(block)).toEqual(expected); |
| 56 | + }); |
| 57 | + |
| 58 | + test('with @ruby:method:p', () => { |
| 59 | + const block = { |
| 60 | + id: 'block-id', |
| 61 | + opcode: 'looks_say', |
| 62 | + inputs: { |
| 63 | + MESSAGE: {} |
| 64 | + } |
| 65 | + }; |
| 66 | + RubyGenerator.cache_.comments['block-id'] = { text: '@ruby:method:p' }; |
| 67 | + RubyGenerator.valueToCode = jest.fn().mockReturnValue('"Hello!"'); |
| 68 | + const expected = 'p("Hello!")\n'; |
| 69 | + expect(RubyGenerator.looks_say(block)).toEqual(expected); |
| 70 | + }); |
| 71 | + |
| 72 | + test('with unknown @ruby: tag defaults to say', () => { |
| 73 | + const block = { |
| 74 | + id: 'block-id', |
| 75 | + opcode: 'looks_say', |
| 76 | + inputs: { |
| 77 | + MESSAGE: {} |
| 78 | + } |
| 79 | + }; |
| 80 | + RubyGenerator.cache_.comments['block-id'] = { text: '@ruby:unknown' }; |
| 81 | + RubyGenerator.valueToCode = jest.fn().mockReturnValue('"Hello!"'); |
| 82 | + const expected = 'say("Hello!")\n'; |
| 83 | + expect(RubyGenerator.looks_say(block)).toEqual(expected); |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + describe('scrub_ (meta-comment filtering)', () => { |
| 88 | + test('should filter out @ruby: comments', () => { |
| 89 | + const block = { |
| 90 | + id: 'block-id', |
| 91 | + opcode: 'looks_say', |
| 92 | + inputs: {}, |
| 93 | + next: null |
| 94 | + }; |
| 95 | + RubyGenerator.cache_.comments['block-id'] = { text: '@ruby:method:print' }; |
| 96 | + RubyGenerator.getInputs = jest.fn().mockReturnValue({}); |
| 97 | + RubyGenerator.isConnectedValue = jest.fn().mockReturnValue(false); |
| 98 | + RubyGenerator.getBlock = jest.fn().mockReturnValue(null); |
| 99 | + RubyGenerator.blockToCode = jest.fn().mockReturnValue(''); |
| 100 | + |
| 101 | + const code = 'print("Hello!")\n'; |
| 102 | + const result = RubyGenerator.scrub_(block, code); |
| 103 | + |
| 104 | + // Should NOT contain the comment since it starts with @ruby: |
| 105 | + expect(result).toEqual('print("Hello!")\n'); |
| 106 | + }); |
| 107 | + |
| 108 | + test('should keep normal comments', () => { |
| 109 | + const block = { |
| 110 | + id: 'block-id', |
| 111 | + opcode: 'looks_say', |
| 112 | + inputs: {}, |
| 113 | + next: null |
| 114 | + }; |
| 115 | + RubyGenerator.cache_.comments['block-id'] = { text: 'normal comment' }; |
| 116 | + RubyGenerator.getInputs = jest.fn().mockReturnValue({}); |
| 117 | + RubyGenerator.isConnectedValue = jest.fn().mockReturnValue(false); |
| 118 | + RubyGenerator.getBlock = jest.fn().mockReturnValue(null); |
| 119 | + RubyGenerator.blockToCode = jest.fn().mockReturnValue(''); |
| 120 | + |
| 121 | + const code = 'say("Hello!")\n'; |
| 122 | + const result = RubyGenerator.scrub_(block, code); |
| 123 | + |
| 124 | + expect(result).toEqual('# normal comment\nsay("Hello!")\n'); |
| 125 | + }); |
| 126 | + }); |
| 127 | +}); |
0 commit comments