Skip to content

Latest commit

 

History

History
315 lines (218 loc) · 6.25 KB

KopytkoExpect.md

File metadata and controls

315 lines (218 loc) · 6.25 KB

KopytkoExpect API

Implementation of the expect function is a bit similar to the jestjs.io one.

It can take any value, or mocked function name as an argument and you can call on it an assert function.

Methods

toBe

Checks equality of two values, or Node reference.

This does not work for object references (only Node).

it("should pass", function () as String
  return expect(2).toBe(2)
end function)

toBeInvalid

Checks if value is invalid.

it("should pass", function () as String
  return expect(Invalid).toBeInvalid()
end function)

toBeValid

Checks if value is valid.

it("should pass", function () as String
  return expect(1).toBeValid()
end function)

toBeTrue

Checks if value is true.

it("should pass", function () as String
  return expect(true).toBeTrue()
end function)

toBeFalse

Checks if value is false.

it("should pass", function () as String
  return expect(false).toBeFalse()
end function)

toEqual

Checks values equality. For primitive values works the same as toBe.

Examples:

For AssociativeArray

it("should pass", function () as String
  return expect({ a: 2 }).toEqual({ a: 2 })
end function)

For Nodes

it("should pass", function () as String
  nodeA = CreateNode("roSGNode", "Rectangle")
  nodeA.id = "asd"
  nodeB = CreateNode("roSGNode", "Rectangle")
  nodeB.id = "asd"

  return expect(nodeA).toEqual(nodeB)
end function)

toContain

Checks if Array/AssociativeArray/Node contains the expected value/subset/node

Examples:

For Array entries

it("should pass", function () as String
  return expect(["a", "b"]).toContain("a")
end function)

For AssociativeArray subset

it("should pass", function () as String
  return expect({ a: 2, b: 5 }).toContain({ a: 2 })
end function)

For Nodes fields

it("should pass", function () as String
  node = CreateNode("roSGNode", "Rectangle")
  node.id = "asd"

  return expect(node).toContain({ id: "asd" })
end function)

For Nodes children

it("should pass", function () as String
  parent = CreateNode("roSGNode", "Rectangle")
  child = CreateNode("roSGNode", "Rectangle")
  parent.appendChild(child)

  return expect(parent).toContain(child)
end function)

toHaveKey

Checks if AssociativeArray contains given key.

it("should pass", function () as String
  return expect({ a: 2 }).toHaveKey("a")
end function)

toHaveKeys

Checks if AssociativeArray contains given keys.

it("should pass", function () as String
  return expect({ a: 2, b: "asd" }).toHaveKeys(["a", "b"])
end function)

toHaveLength

Checks if Array or AssociativeArray has given lenght/count.

it("should pass", function () as String
  return expect({ a: 2, b: "asd" }).toHaveLength(2)
end function)

toHaveBeenCalled

Checks if mocked function was called at least once.

' @mock /path/to/functionName


it("should pass", function () as String
  return expect("functionName").toHaveBeenCalled()
end function)

toHaveBeenCalledTimes

Checks if mocked function was called given times.

' @mock /path/to/functionName


it("should pass", function () as String
  return expect("functionName").toHaveBeenCalledTimes(1)
end function)

toHaveBeenCalledWith

Checks if mocked function was called with given arguments.

Checks if mocked function was called given times.

' @mock /path/to/functionName


it("should pass", function () as String
  return expect("functionName").toHaveBeenCalledWith({ a: 2 })
end function)

Strict mode

By default, this function is not in strict mode. So it will validate only against given arguments. With strict mode, it checks if all arguments are identical So this will throw an error.

' @mock /path/to/functionWithThreeArguments


it("should pass", function () as String
  return expect("functionWithThreeArguments").toHaveBeenCalledWith({ a: 2, b: 3 }, { strict: true })
end function)

toHaveBeenLastCalledWith

Checks if mocked function last call was with given arguments.

' @mock /path/to/functionName


it("should pass", function () as String
  return expect("functionName").toHaveBeenLastCalledWith({ a: 2 })
end function)

Strict mode

By default, this function is not in strict mode. So it will validate only against given arguments. With strict mode, it checks if all arguments are identical So this will throw an error.

' @mock /path/to/functionWithThreeArguments


it("should pass", function () as String
  return expect("functionWithThreeArguments").toHaveBeenLastCalledWith({ a: 2, b: 3 }, { strict: true })
end function)

toHaveBeenNthCalledWith

Checks if mocked function nth, given, call was executed with given arguments.

' @mock /path/to/functionName


it("should pass", function () as String
  return expect("functionName").toHaveBeenNthCalledWith(1, { a: 2 })
end function)

Strict mode

By default, this function is not in strict mode. So it will validate only against given arguments. With strict mode, it checks if all arguments are identical So this will throw an error.

' @mock /path/to/functionWithThreeArguments


it("should pass", function () as String
  return expect("functionWithThreeArguments").toHaveBeenNthCalledWith(1, { a: 2, b: 3 }, { strict: true })
end function)

toThrow

Checks if the function throws an exception.

it("should pass", function () as String
  return expect(functionThatThrow).toThrow("Error message")
end function)

not

Check the opposite of the assert function.

it("should pass", function () as String
  return expect(1)not.toBe(4)
end function)