Commit 4cb2afd9 authored by David Owens II's avatar David Owens II

Moved the shared 'test' infra to main.swift.

Added Scanner and the most basic of tests.
parent 950d83f1
// © 2016 Anarchy Tools Contributors.
// This file is part of atbuild. It is subject to the license terms in the LICENSE
// file found in the top level of this distribution
// No part of atbuild, including this file, may be copied, modified,
// propagated, or distributed except according to the terms contained
// in the LICENSE file.
struct ScannerInfo {
let character: Character?
let line: Int
let column: Int
}
class Scanner {
var content: String
var index: String.Index
var current: ScannerInfo? = nil
private var shouldStall = false
var line: Int = 0
var column: Int = 0
init(content: String) {
self.content = content
self.index = content.startIndex
self._defaults()
}
func _defaults() {
self.index = content.startIndex
self.line = 0
self.column = 0
self.shouldStall = false
self.current = nil
}
func stall() {
shouldStall = true
}
func next() -> ScannerInfo? {
if shouldStall {
shouldStall = false
return current
}
if index == content.endIndex {
current = nil
}
else {
current = ScannerInfo(character: content[index], line: line, column: column)
index = index.successor()
if current?.character == "\n" {
line += 1
column = 0
}
else {
column += 1
}
}
return current
}
func peek() -> ScannerInfo? {
return current
}
func debugPrint() {
print("--- SCANNER INFO ---")
while let info = self.next() {
print("\(info)")
}
print("")
self._defaults()
}
}
\ No newline at end of file
......@@ -5,38 +5,23 @@
// propagated, or distributed except according to the terms contained
// in the LICENSE file.
extension String: ErrorType {}
enum test {
static func assert(condition: Bool, functionName: String = __FUNCTION__) throws {
print(" \(functionName): \(condition ? "PASSED" : "**FAILED**")")
if !condition { throw "clj.tests.failed" }
}
}
class RingBufferTests: Test {
required init() {}
func runTests() {
print("Tests for \(__FILE__)")
let tests = [
testBasicInit,
testInsertion,
testRemove,
testNoOverwrite
]
let tests = [
RingBufferTests.testBasicInit,
RingBufferTests.testInsertion,
RingBufferTests.testRemove,
RingBufferTests.testNoOverwrite
]
for test in tests {
let _ = try? test()
}
}
func testBasicInit() throws {
let filename: String = __FILE__
static func testBasicInit() throws {
let buffer = RingBuffer<Int>(capacity: 10)
try test.assert(buffer.capacity == 10)
}
func testInsertion() throws {
static func testInsertion() throws {
let buffer = RingBuffer<Int>(capacity: 3)
try test.assert(buffer.capacity == 3)
......@@ -53,7 +38,7 @@ class RingBufferTests: Test {
try test.assert(buffer[0] == 103)
}
func testRemove() throws {
static func testRemove() throws {
let buffer = RingBuffer<Int>(capacity: 3)
try test.assert(buffer.capacity == 3)
......@@ -83,7 +68,7 @@ class RingBufferTests: Test {
try test.assert(buffer[2] == nil)
}
func testNoOverwrite() throws {
static func testNoOverwrite() throws {
let buffer = RingBuffer<Int>(capacity: 3, overwrite: false)
try test.assert(buffer.capacity == 3)
......
// © 2016 Anarchy Tools Contributors.
// This file is part of atbuild. It is subject to the license terms in the LICENSE
// file found in the top level of this distribution
// No part of atbuild, including this file, may be copied, modified,
// propagated, or distributed except according to the terms contained
// in the LICENSE file.
class ScannerTests: Test {
required init() {}
let tests = [
ScannerTests.testBasicInit
]
let filename = __FILE__
static func testBasicInit() throws {
let scanner = Scanner(content: "")
try test.assert(scanner.content == "")
try test.assert(scanner.current == nil)
}
}
\ No newline at end of file
......@@ -5,15 +5,41 @@
// propagated, or distributed except according to the terms contained
// in the LICENSE file.
// NOTE: This is the crappiest test thing ever... but it works for now.
extension String : ErrorType {}
enum test {
static func assert(condition: Bool, functionName: String = __FUNCTION__) throws {
print(" \(functionName): \(condition ? "PASSED" : "**FAILED**")")
if !condition { throw "clj.tests.failed" }
}
}
protocol Test {
init()
func runTests()
var tests: [() throws -> ()] { get }
var filename: String { get }
}
extension Test {
func runTests() {
print("Tests for \(__FILE__)")
for test in tests {
let _ = try? test()
}
}
}
print()
let tests: [Test] = [
RingBufferTests()
// NOTE: Add your test classes here...
RingBufferTests(),
ScannerTests()
]
for test in tests {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment