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
62 changes: 62 additions & 0 deletions src/stdlib/__tests__/__snapshots__/misc.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`parse_int with non-integer arg radix throws error 1`] = `
Object {
"status": "error",
}
`;

exports[`parse_int with non-integer arg radix throws error 2`] = `"Line 2: Error: parse_int expects two arguments a string s, and a positive integer i between 2 and 36, inclusive."`;

exports[`parse_int with non-string arg str throws error 1`] = `
Object {
"status": "error",
}
`;

exports[`parse_int with non-string arg str throws error 2`] = `"Line 2: Error: parse_int expects two arguments a string s, and a positive integer i between 2 and 36, inclusive."`;

exports[`parse_int with radix outside [2, 36] throws error, radix=1 1`] = `
Object {
"status": "error",
}
`;

exports[`parse_int with radix outside [2, 36] throws error, radix=1 2`] = `"Line 2: Error: parse_int expects two arguments a string s, and a positive integer i between 2 and 36, inclusive."`;

exports[`parse_int with radix outside [2, 36] throws error, radix=37 1`] = `
Object {
"status": "error",
}
`;

exports[`parse_int with radix outside [2, 36] throws error, radix=37 2`] = `"Line 2: Error: parse_int expects two arguments a string s, and a positive integer i between 2 and 36, inclusive."`;

exports[`parse_int with string arg radix throws error 1`] = `
Object {
"status": "error",
}
`;

exports[`parse_int with string arg radix throws error 2`] = `"Line 2: Error: parse_int expects two arguments a string s, and a positive integer i between 2 and 36, inclusive."`;

exports[`parse_int with valid args is ok, but invalid str for radix 1`] = `
Object {
"status": "finished",
"value": NaN,
}
`;

exports[`parse_int with valid args is ok, radix 2 1`] = `
Object {
"status": "finished",
"value": 6485,
}
`;

exports[`parse_int with valid args is ok, radix 36 1`] = `
Object {
"status": "finished",
"value": 39961,
}
`;
112 changes: 112 additions & 0 deletions src/stdlib/__tests__/misc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import { mockContext } from "../../mocks/context"
import { parseError, runInContext } from "../../index"
import { Finished } from "../../types";

test("parse_int with valid args is ok, radix 2", () => {
const program = `
parse_int('1100101010101', 2);
`
const context = mockContext()
const promise = runInContext(program, context, { scheduler: "preemptive" })
return promise.then(obj => {
expect(obj).toMatchSnapshot()
expect(obj.status).toBe('finished')
expect((obj as Finished).value).toBe(parseInt('1100101010101', 2))
})
})

test("parse_int with valid args is ok, radix 36", () => {
const program = `
parse_int('uu1', 36);
`
const context = mockContext()
const promise = runInContext(program, context, { scheduler: "preemptive" })
return promise.then(obj => {
expect(obj).toMatchSnapshot()
expect(obj.status).toBe('finished')
expect((obj as Finished).value).toBe(parseInt('uu1', 36))
})
})

test("parse_int with valid args is ok, but invalid str for radix", () => {
const program = `
parse_int('uu1', 2);
`
const context = mockContext()
const promise = runInContext(program, context, { scheduler: "preemptive" })
return promise.then(obj => {
expect(obj).toMatchSnapshot()
expect(obj.status).toBe('finished')
expect((obj as Finished).value).toBe(parseInt('uu1', 2))
})
})

test("parse_int with non-string arg str throws error", () => {
const program = `
parse_int(42, 2);
`
const context = mockContext()
const promise = runInContext(program, context, { scheduler: "preemptive" })
return promise.then(obj => {
expect(obj).toMatchSnapshot()
expect(obj.status).toBe('error')
const errors = parseError(context.errors)
expect(errors).toMatchSnapshot()
})
})

test("parse_int with non-integer arg radix throws error", () => {
const program = `
parse_int(42, 2.1);
`
const context = mockContext()
const promise = runInContext(program, context, { scheduler: "preemptive" })
return promise.then(obj => {
expect(obj).toMatchSnapshot()
expect(obj.status).toBe('error')
const errors = parseError(context.errors)
expect(errors).toMatchSnapshot()
})
})

test("parse_int with radix outside [2, 36] throws error, radix=1", () => {
const program = `
parse_int('10', 1);
`
const context = mockContext()
const promise = runInContext(program, context, { scheduler: "preemptive" })
return promise.then(obj => {
expect(obj).toMatchSnapshot()
expect(obj.status).toBe('error')
const errors = parseError(context.errors)
expect(errors).toMatchSnapshot()
})
})

test("parse_int with radix outside [2, 36] throws error, radix=37", () => {
const program = `
parse_int('10', 37);
`
const context = mockContext()
const promise = runInContext(program, context, { scheduler: "preemptive" })
return promise.then(obj => {
expect(obj).toMatchSnapshot()
expect(obj.status).toBe('error')
const errors = parseError(context.errors)
expect(errors).toMatchSnapshot()
})
})

test("parse_int with string arg radix throws error", () => {
const program = `
parse_int(42, '2');
`
const context = mockContext()
const promise = runInContext(program, context, { scheduler: "preemptive" })
return promise.then(obj => {
expect(obj).toMatchSnapshot()
expect(obj.status).toBe('error')
const errors = parseError(context.errors)
expect(errors).toMatchSnapshot()
})
})
19 changes: 13 additions & 6 deletions src/stdlib/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,20 @@ export function array_length(xs: Value[]) {
}
array_length.__SOURCE__ = 'array_length(xs)'

export function parse_int(inputString: string, radix: number) {
const parsed = parseInt(inputString, radix)
if (inputString && radix && parsed) {
// the two arguments are provided, and parsed is not NaN
return parsed
/**
* Source version of parseInt. Both arguments are required.
*
* @param str String representation of the integer to be parsed. Required.
* @param radix Base to parse the given `str`. Required.
*
* An error is thrown if `str` is not of type string, or `radix` is not an
* integer within the range 2, 36 inclusive.
*/
export function parse_int(str: string, radix: number) {
if (typeof(str) === 'string' && typeof(radix) === 'number' && Number.isInteger(radix) && 2 <= radix && radix <= 36) {
return parseInt(str, radix)
} else {
throw new Error('parse_int expects two arguments a string s, and a positive integer i')
throw new Error('parse_int expects two arguments a string s, and a positive integer i between 2 and 36, inclusive.')
}
}
parse_int.__SOURCE__ = 'parse_int(s, i)'
Expand Down