Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed handling of arrays for ROWS/COLUMNS functions. (#677)
- Fixed an issue with nested namedexpressions. (#679)
- Fixed an issue with matrixDetection + number parsing. (#686)
- Fixed an issue with NOW and TODAY functions. (#709)

## [0.6.2] - 2021-05-26

Expand Down
8 changes: 4 additions & 4 deletions src/interpreter/plugin/DateTimePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,18 +444,18 @@ export class DateTimePlugin extends FunctionPlugin implements FunctionPluginType
public now(ast: ProcedureAst, state: InterpreterState): InterpreterValue {
return this.runFunction(ast.args, state, this.metadata('NOW'),
() => {
const now = new Date()
const now = new Date(Date.now())
return timeToNumber({hours: now.getHours(), minutes: now.getMinutes(), seconds: now.getSeconds()})+
this.interpreter.dateHelper.dateToNumber({year: now.getFullYear(), month: now.getMonth()+1, day: now.getDay()})
this.interpreter.dateHelper.dateToNumber({year: now.getFullYear(), month: now.getMonth()+1, day: now.getDate()})
}
)
}

public today(ast: ProcedureAst, state: InterpreterState): InterpreterValue {
return this.runFunction(ast.args, state, this.metadata('TODAY'),
() => {
const now = new Date()
return this.interpreter.dateHelper.dateToNumber({year: now.getFullYear(), month: now.getMonth()+1, day: now.getDay()})
const now = new Date(Date.now())
return this.interpreter.dateHelper.dateToNumber({year: now.getFullYear(), month: now.getMonth()+1, day: now.getDate()})
}
)
}
Expand Down
57 changes: 53 additions & 4 deletions test/interpreter/function-now.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,62 @@ function sleepFor( sleepDuration: number ){
}

describe('Interpreter - function NOW', () => {
let originalNow: () => number

beforeEach(() => {
originalNow = Date.now
let cnt = 20
Date.now = () => {
cnt += 1
return Date.parse(`1985-08-16T03:45:${cnt}`)
}
Comment on lines +16 to +20

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A more elegant way will be and it's clear its a mock:

Suggested change
let cnt = 20
Date.now = () => {
cnt += 1
return Date.parse(`1985-08-16T03:45:${cnt}`)
}
dateMock = jest.fn().mockReturnValue(new Date('1985-08-16T03:45:30Z').getTime())
Date.now = dateMock

I will get to NOW in the next comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will it work for the multitude of testers that we have? (jest, jasmine, karma)?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true, it will not, I forgot about jasmine

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should fight for some hybrid jest/jasmine mock tests and just go with what we have. But of course it is up to you

})

it('works', () => {
const engine = HyperFormula.buildFromArray([
['=NOW()'],
])
const t1 = engine.getCellValue(adr('A1')) as number
expect(t1).toBeCloseTo(31275.1565856481)
expect(engine.getCellValueDetailedType(adr('A1'))).toBe(CellValueDetailedType.NUMBER_DATETIME)
sleepFor(1000)
engine.setCellContents(adr('A2'), null)
const t2 = engine.getCellValue(adr('A1')) as number
const delta = (t2-t1)*(24*60*60)
expect(delta).toBeCloseTo(1)
const delta = engine.getCellValue(adr('A1')) as number - t1
expect(delta*24*60*60).toBeGreaterThanOrEqual(1) //internals of the engine are also using Date.now(), so the value should be actually 4 or even more
Comment on lines +28 to +32

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be better to have this test predictable?

dateMock.mockReturnValueOnce(new Date('1985-08-16T03:45:30Z').getTime())

const t1 = engine.getCellValue(adr('A1'))

dateMock.mockReturnValueOnce(new Date('1985-08-16T03:45:30Z').getTime())

engine.setCellContents(adr('A2'), null)
const t2 = engine.getCellValue(adr('A1'))

we will know exactly what time each NOW() returns and can make precise expectation

})

it('works #2', () => {
const engine = HyperFormula.buildFromArray([
['=YEAR(NOW())'],
])
expect(engine.getCellValue(adr('A1'))).toEqual(1985)
})

it('works #3', () => {
const engine = HyperFormula.buildFromArray([
['=MONTH(NOW())'],
])
expect(engine.getCellValue(adr('A1'))).toEqual(8)
})

it('works #4', () => {
const engine = HyperFormula.buildFromArray([
['=DAY(NOW())'],
])
expect(engine.getCellValue(adr('A1'))).toEqual(16)
})

it('works #5', () => {
const engine = HyperFormula.buildFromArray([
['=HOUR(NOW())'],
])
expect(engine.getCellValue(adr('A1'))).toEqual(3)
})

it('works #6', () => {
const engine = HyperFormula.buildFromArray([
['=MINUTE(NOW())'],
])
expect(engine.getCellValue(adr('A1'))).toEqual(45)
})

it('validates number of arguments', () => {
Expand All @@ -29,4 +74,8 @@ describe('Interpreter - function NOW', () => {

expect(engine.getCellValue(adr('A1'))).toEqualError(detailedError(ErrorType.NA, ErrorMessage.WrongArgNumber))
})

afterEach(() => {
Date.now = originalNow
})
})
38 changes: 37 additions & 1 deletion test/interpreter/function-today.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,55 @@ import {ErrorMessage} from '../../src/error-message'
import {adr, detailedError} from '../testUtils'

describe('Interpreter - function TODAY', () => {
let originalNow: () => number

beforeEach(() => {
originalNow = Date.now
let cnt = 20
Date.now = () => {
cnt += 1
return Date.parse(`1985-08-16T03:45:${cnt}`)
}
})

it('works', () => {
const engine = HyperFormula.buildFromArray([
['=TODAY()'],
])
expect(engine.getCellValue(adr('A1'))).not.toBeNull()
expect(engine.getCellValue(adr('A1'))).toEqual(31275)
expect(engine.getCellValueDetailedType(adr('A1'))).toBe(CellValueDetailedType.NUMBER_DATE)
})

it('works #2', () => {
const engine = HyperFormula.buildFromArray([
['=YEAR(TODAY())'],
])
expect(engine.getCellValue(adr('A1'))).toEqual(1985)
})

it('works #3', () => {
const engine = HyperFormula.buildFromArray([
['=MONTH(TODAY())'],
])
expect(engine.getCellValue(adr('A1'))).toEqual(8)
})

it('works #4', () => {
const engine = HyperFormula.buildFromArray([
['=DAY(TODAY())'],
])
expect(engine.getCellValue(adr('A1'))).toEqual(16)
})

it('validates number of arguments', () => {
const engine = HyperFormula.buildFromArray([
['=TODAY(42)'],
])

expect(engine.getCellValue(adr('A1'))).toEqualError(detailedError(ErrorType.NA, ErrorMessage.WrongArgNumber))
})

afterEach(() => {
Date.now = originalNow
})
})