Skip to content

Commit

Permalink
Move stringification of display output to Process
Browse files Browse the repository at this point in the history
This allows me to test the stringification logic, and also means
simulate() tests can rely on the simulated display output being an
array of strings.
  • Loading branch information
Ben Christel committed Oct 11, 2018
1 parent 2fe9dd9 commit 972bc9c
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/core/Core.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ describe('Core', () => {
})
`)
view = core.run()
expect(view.displayLines).toEqual([0])
expect(view.displayLines).toEqual(['0'])
view = core.tickFrames(60)
expect(view.displayLines).toEqual([3])
expect(view.displayLines).toEqual(['3'])
})

it('outputs the stack trace on a crash', () => {
Expand Down
15 changes: 12 additions & 3 deletions src/core/Process.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { isIterator, isGeneratorFunction } from './nativeTypes'
import { lastOf } from './sequences'
import { isIterator, isGeneratorFunction, isArray } from './nativeTypes'
import { doWith } from './functionalUtils'
import { lastOf, map } from './sequences'
import { asText } from './formatting'

export function Process(store) {
let stack = []
Expand Down Expand Up @@ -157,7 +159,10 @@ export function Process(store) {
}
}

displayLines = render ? render(store.getState()) : []
displayLines = doWith(
render ? render(store.getState()) : [],
asArray,
map(asText))
}

function view() {
Expand All @@ -169,3 +174,7 @@ export function Process(store) {
}
}
}

function asArray(value) {
return isArray(value) ? value : [value]
}
6 changes: 3 additions & 3 deletions src/core/Process.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ describe('Process', () => {
})
x++
})
expect(view.displayLines).toEqual([1])
expect(view.displayLines).toEqual(['1'])
})

it('passes the state to the render function', () => {
Expand Down Expand Up @@ -424,8 +424,8 @@ describe('Process', () => {
yield startDisplay(() => [redraws++])
yield wait(1)
})
expect(view.displayLines).toEqual([0])
expect(view.displayLines).toEqual(['0'])
view = p.redraw()
expect(view.displayLines).toEqual([1])
expect(view.displayLines).toEqual(['1'])
})
})
9 changes: 9 additions & 0 deletions src/core/sequences.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { isArray, isString } from './nativeTypes'
import { checkArgs } from './checkArgs'
import { or } from './predicates'
import { partialApply } from './higherOrderFunctions'

const lastOf_interface = {
example: [[1, 2, 3]],
Expand All @@ -11,3 +12,11 @@ export function lastOf(list) {
checkArgs(lastOf, arguments, lastOf_interface)
return list[list.length - 1]
}

export function map(fn, seq) {
if (arguments.length < 2) {
return partialApply(map, arguments)
}
if (seq.length === 0) return seq
return seq.map((item) => fn(item))
}
22 changes: 21 additions & 1 deletion src/core/sequences.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { lastOf } from './sequences'
import { lastOf, map } from './sequences'

describe('lastOf', function() {
it('returns undefined for an empty array', () => {
Expand All @@ -17,3 +17,23 @@ describe('lastOf', function() {
expect(lastOf('abc')).toBe('c')
})
})

describe('map', function() {
function testFn(item) {
return item + 1
}

it('returns an empty array unchanged', () => {
let a = []
expect(map(testFn, a)).toBe(a)
})

it('transforms each array item', () => {
expect(map(testFn, [1])).toEqual([2])
expect(map(testFn, [1, 2, 3])).toEqual([2, 3, 4])
})

it('curries', () => {
expect(map(testFn)([1])).toEqual([2])
})
})
10 changes: 6 additions & 4 deletions src/ui/components/Terminal.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React from 'react'
import connectProps from './connectProps'
import './Terminal.css'
import Pane from './Pane'
import { asText } from '../../core/formatting'

export default connectProps(class extends React.Component {
bottomOfLogs = null
Expand All @@ -21,9 +20,13 @@ export default connectProps(class extends React.Component {
}
}

screenLines() {
return this.props.appUi.screenLines
}

render() {
let classNames = "Terminal"
if (this.props.appUi.screenLines.length) classNames += ' showScreen'
if (this.screenLines().length) classNames += ' showScreen'
return (
<Pane style={{height: '608px', width: '100%'}}>
<div className={classNames}>
Expand All @@ -36,8 +39,7 @@ export default connectProps(class extends React.Component {
</div>
<div className="screen">
{
this.props.appUi.screenLines
.map(asText)
this.screenLines()
.map((line, i) =>
<div className="line" key={i}>{line}</div>)
}
Expand Down

0 comments on commit 972bc9c

Please sign in to comment.