Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"gapi",
"googleusercontent",
"Hira",
"Interconvert",
"mbit",
"smalrubot"
],
Expand Down
9 changes: 8 additions & 1 deletion src/lib/ruby-generator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,14 @@ RubyGenerator.scrubNakedValue = function (line) {

RubyGenerator.escapeChars_ = {
'"': '\\"',
'\\': '\\\\'
'\\': '\\\\',
'\n': '\\n',
'\t': '\\t',
'\r': '\\r',
'\b': '\\b',
'\f': '\\f',
'\v': '\\v',
'\0': '\\0'
};

RubyGenerator.quote_ = function (string) {
Expand Down
29 changes: 0 additions & 29 deletions test/integration/ruby-tab/operators.test.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
import dedent from 'dedent';
import SeleniumHelper from '../../helpers/selenium-helper';
import RubyHelper from '../../helpers/ruby-helper';
import {EDIT_MENU_XPATH} from '../../helpers/menu-xpaths';

const seleniumHelper = new SeleniumHelper();
const {
clickText,
clickXpath,
getDriver,
loadUri,
urlFor
} = seleniumHelper;

const rubyHelper = new RubyHelper(seleniumHelper);
const {
fillInRubyProgram,
currentRubyProgram,
expectInterconvertBetweenCodeAndRuby
} = rubyHelper;

Expand Down Expand Up @@ -98,28 +93,4 @@ describe('Ruby Tab: Operators category blocks', () => {
`;
await expectInterconvertBetweenCodeAndRuby(code);
});

test('Ruby -> Code -> Ruby (escape characters) ', async () => {
await loadUri(urlFor('/'));

const beforeRuby = dedent`
"\\\\" + "\\\\"

"\\n" + "\\n"
`;

const afterRuby = dedent`
"\\" + "\\"

"\n" + "\n"
`;

await clickText('Ruby', '*[@role="tab"]');
await fillInRubyProgram(beforeRuby);
await clickText('Code', '*[@role="tab"]');
await clickXpath(EDIT_MENU_XPATH);
await clickText('Generate Ruby from Code');
await clickText('Ruby', '*[@role="tab"]');
expect(await currentRubyProgram()).toEqual(`${afterRuby}\n`);
});
});
50 changes: 50 additions & 0 deletions test/unit/lib/ruby-generator/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import RubyGenerator from '../../../../src/lib/ruby-generator';

describe('RubyGenerator', () => {
describe('quote_', () => {
test('should escape double quotes', () => {
const result = RubyGenerator.quote_('"');
expect(result).toBe('"\\\""');
});

test('should escape backslashes', () => {
const result = RubyGenerator.quote_('\\');
expect(result).toBe('"\\\\"');
});

test('should escape newline characters', () => {
const result = RubyGenerator.quote_('\n');
expect(result).toBe('"\\n"');
});

test('should escape tab characters', () => {
const result = RubyGenerator.quote_('\t');
expect(result).toBe('"\\t"');
});

test('should escape carriage return characters', () => {
const result = RubyGenerator.quote_('\r');
expect(result).toBe('"\\r"');
});

test('should escape backspace characters', () => {
const result = RubyGenerator.quote_('\b');
expect(result).toBe('"\\b"');
});

test('should escape form feed characters', () => {
const result = RubyGenerator.quote_('\f');
expect(result).toBe('"\\f"');
});

test('should escape vertical tab characters', () => {
const result = RubyGenerator.quote_('\v');
expect(result).toBe('"\\v"');
});

test('should escape null characters', () => {
const result = RubyGenerator.quote_('\0');
expect(result).toBe('"\\0"');
});
});
});
24 changes: 24 additions & 0 deletions test/unit/lib/ruby-generator/looks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,30 @@ describe('RubyGenerator/Looks', () => {
const expected = 'say("Hello!")\n';
expect(RubyGenerator.looks_say(block)).toEqual(expected);
});

test('print with newline character', () => {
const block = {
id: 'block-id',
opcode: 'looks_say',
inputs: { MESSAGE: {} }
};
RubyGenerator.cache_.comments['block-id'] = { text: '@ruby:method:print' };
RubyGenerator.valueToCode = jest.fn().mockReturnValue('"Hello, Ruby.\\n"');
const expected = 'print("Hello, Ruby.\\n")\n';
expect(RubyGenerator.looks_say(block)).toEqual(expected);
});

test('puts with tab character', () => {
const block = {
id: 'block-id',
opcode: 'looks_say',
inputs: { MESSAGE: {} }
};
RubyGenerator.cache_.comments['block-id'] = { text: '@ruby:method:puts' };
RubyGenerator.valueToCode = jest.fn().mockReturnValue('"Hello\\tRuby"');
const expected = 'puts("Hello\\tRuby")\n';
expect(RubyGenerator.looks_say(block)).toEqual(expected);
});
});

describe('scrub_ (meta-comment filtering)', () => {
Expand Down
Loading