const test = require('tape')
const BorrowState = require('../lib/module.js')

const countTo = require('./count-to.js')

test(`read-only-safety`, (t) => {
  t.plan(2)
  let hasHappened = []
  let myState = new BorrowState()
  myState.block().then((state) => {
    hasHappened.push(1)
    state.foo = Math.PI
    state.unblock()
  })
  myState.block('r').then((state) => {
    hasHappened.push(2)
    state.foo = 5
    state.unblock()
  })
  // The following is not set to read-only so that it can occur *after* the previous
  myState.block().then((state) => {
    hasHappened.push(3)
    t.equal(state.foo, Math.PI, 'The state should still be set to pi')
    t.equal(hasHappened, countTo(3), 'The operations happened in the right order')
    state.unblock()
  })
})

test(`read-only-safety, chained`, (t) => {
  t.plan(2)
  let hasHappened = []
  let myState = new BorrowState()
  myState.block().then((state) => {
    hasHappened.push(1)
    state.foo = Math.PI
    state.unblock()
  })
  myState.block('r').then((state) => {
    hasHappened.push(2)
    state.foo = 5
    return state
  }).then((state) => {
    hasHappened.push(3)
    t.equal(state.foo, Math.PI, 'The state should still be set to pi')
    t.equal(hasHappened, countTo(3), 'The operations happened in the right order')
    state.unblock()
  })
})

@@ -284,39 +284,33 @@
     state.foo = 
-Math.PI
+0
 %0A    state.unblo
@@ -585,39 +585,33 @@
 qual(state.foo, 
-Math.PI
+0
 , 'The state sho
@@ -935,23 +935,17 @@
 e.foo = 
-Math.PI
+0
 %0A    sta
@@ -1134,15 +1134,9 @@
 oo, 
-Math.PI
+0
 , 'T

Use a rational number