PackageTests.swift 7.61 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// Copyright (c) 2016 Anarchy Tools Contributors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


import Foundation
import atpkg

class PackageTests: Test {
    required init() {}
    let tests = [
22
        PackageTests.testBasic,
Drew's avatar
Drew committed
23
        PackageTests.testImport,
Drew's avatar
Drew committed
24
        PackageTests.testOverlays,
Drew's avatar
Drew committed
25 26
        PackageTests.testExportedOverlays,
        PackageTests.testChainedImports
27 28 29 30 31
    ]

    let filename = __FILE__
    
    static func testBasic() throws {
Drew's avatar
Drew committed
32
        let filepath = "./tests/collateral/basic.atpkg"
33 34 35 36 37 38

        guard let parser = Parser(filepath: filepath) else {
            try test.assert(false); return
        }
        
        let result = try parser.parse()
39
        guard let package = Package(type: result, overlay: [], pathOnDisk: "./tests/collateral") else { try test.assert(false); return }
40 41 42 43
        
        try test.assert(package.name == "basic")
        try test.assert(package.version == "0.1.0-dev")
        
Drew's avatar
Drew committed
44
        try test.assert(package.tasks.count == 2) //indexed twice, by qualified and unqualified name
45
        for (key, task) in package.tasks {
Drew's avatar
Drew committed
46
            try test.assert(key == "build" || key == "basic.build")
47
            try test.assert(task.tool == "lldb-build")
48 49 50 51 52
            try test.assert(task["name"]?.string == "json-swift")
            try test.assert(task["output-type"]?.string == "lib")
            try test.assert(task["source"]?.vector?.count == 2)
            try test.assert(task["source"]?.vector?[0].string == "src/**.swift")
            try test.assert(task["source"]?.vector?[1].string == "lib/**.swift")
53 54
        }
    }
55 56 57

    static func testImport() throws {
        let filepath = "./tests/collateral/import_src.atpkg"
58
        guard let package = Package(filepath: filepath, overlay: []) else { print("error"); try test.assert(false); return }
59 60

        try test.assert(package.tasks["import_dst.build"] != nil)
Drew's avatar
Drew committed
61
        try test.assert(package.tasks["import_dst.build"]!.importedPath == "./tests/collateral/")
62
    }
Drew's avatar
Drew committed
63

Drew's avatar
Drew committed
64 65
    static func testOverlays() throws {
        let filepath = "./tests/collateral/overlays.atpkg"
66
        guard let package = Package(filepath: filepath, overlay: []) else { print("error"); try test.assert(false); return }
Drew's avatar
Drew committed
67 68 69
        guard let compileOptions = package.tasks["build"]?["compileOptions"]?.vector else {
            fatalError("No compile options?")
        }
70
        try test.assert(compileOptions.count == 2)
Drew's avatar
Drew committed
71 72 73
        try test.assert(compileOptions[0].string == "-D")
        try test.assert(compileOptions[1].string == "AWESOME")

74
        guard let package2 = Package(filepath: filepath, overlay: ["more-awesome"]) else { print("error"); try test.assert(false); return }
Drew's avatar
Drew committed
75
        guard let compileOptions2 = package2.tasks["build"]?["compileOptions"]?.vector else {
Drew's avatar
Drew committed
76 77
            fatalError("no compile options?")
        }
78
        try test.assert(compileOptions2.count == 4)
Drew's avatar
Drew committed
79 80 81 82
        try test.assert(compileOptions2[0].string == "-D")
        try test.assert(compileOptions2[1].string == "AWESOME")
        try test.assert(compileOptions2[2].string == "-D")
        try test.assert(compileOptions2[3].string == "MORE_AWESOME")
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102

        guard let package3 = Package(filepath: filepath, overlay: ["most-taskspecific"]) else { print("error"); try test.assert(false); return }
        guard let compileOptions3 = package3.tasks["build"]?["compileOptions"]?.vector else {
            fatalError("no compile options?")
        }
        try test.assert(compileOptions3.count == 4)
        try test.assert(compileOptions3[0].string == "-D")
        try test.assert(compileOptions3[1].string == "AWESOME")
        try test.assert(compileOptions3[2].string == "-D")
        try test.assert(compileOptions3[3].string == "MOST_AWESOME")

        guard let package4 = Package(filepath: filepath, overlay: ["most-taskspecific-two"]) else { print("error"); try test.assert(false); return }
        guard let compileOptions4 = package4.tasks["build"]?["compileOptions"]?.vector else {
            fatalError("no compile options?")
        }
        try test.assert(compileOptions4.count == 4)
        try test.assert(compileOptions4[0].string == "-D")
        try test.assert(compileOptions4[1].string == "AWESOME")
        try test.assert(compileOptions4[2].string == "-D")
        try test.assert(compileOptions4[3].string == "MOST_AWESOME")
Drew's avatar
Drew committed
103

Drew's avatar
Drew committed
104 105 106 107
        guard let package5 = Package(filepath: filepath, overlay: ["stringOption"]) else { print("error"); try test.assert(false); return }
        guard let stringOption = package5.tasks["build"]?["stringOption"]?.string else {
            fatalError("no string option?")
        }
108 109 110 111 112 113 114 115 116
        try test.assert(stringOption == "stringOption")

        guard let package6 = Package(filepath: filepath, overlay: ["emptyVecOption"]) else { print("error"); try test.assert(false); return }
        guard let vecOption = package6.tasks["build"]?["emptyVecOption"]?.vector else {
            fatalError("no vec option?")
        }
        try test.assert(vecOption.count == 1)

        try test.assert(vecOption[0].string == "OVERLAY")
Drew's avatar
Drew committed
117

Drew's avatar
Drew committed
118 119 120 121 122
        guard let package7 = Package(filepath: filepath, overlay: ["boolOption"]) else { print("error"); try test.assert(false); return }
        guard let boolOption = package7.tasks["build"]?["boolOption"]?.bool else {
            fatalError("no bool option?")
        }
        try test.assert(boolOption == true)
Drew's avatar
Drew committed
123
    }
Drew's avatar
Drew committed
124

Drew's avatar
Drew committed
125 126
    static func testExportedOverlays() throws {
        let filepath = "./tests/collateral/overlays_src.atpkg"
Drew's avatar
Drew committed
127

128
        guard let package2 = Package(filepath: filepath, overlay: []) else { print("error"); try test.assert(false); return }
Drew's avatar
Drew committed
129 130 131 132 133 134 135 136 137 138 139 140
        guard let compileOptions2 = package2.tasks["build"]?["compileOptions"]?.vector else {
            fatalError("no compile options?")
        }
        try test.assert(compileOptions2.count == 6)
        try test.assert(compileOptions2[0].string == "-D")
        try test.assert(compileOptions2[1].string == "AWESOME")
        try test.assert(compileOptions2[2].string == "-D")
        try test.assert(compileOptions2[3].string == "MORE_AWESOME")
        try test.assert(compileOptions2[4].string == "-D")
        try test.assert(compileOptions2[5].string == "MOST_AWESOME")

    }
Drew's avatar
Drew committed
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164

    static func testChainedImports () throws {
        let filepath = "./tests/collateral/chained_imports/a.atpkg"
        guard let package = Package(filepath: filepath, overlay: []) else { print("error"); try test.assert(false); return }
        guard let a_default_unqualified = package.tasks["default"] else {
            fatalError("No default task")
        }
        try test.assert(a_default_unqualified["name"]?.string == "a_default")

        guard let a_default_qualified = package.tasks["a.default"] else {
            fatalError("No default task (qualified)")
        }
        try test.assert(a_default_qualified["name"]?.string == "a_default")

        guard let b_default_qualified = package.tasks["b.default"] else {
            fatalError("No default task in b")
        }
        try test.assert(b_default_qualified["name"]?.string == "b_default")

        guard let c_default_qualified = package.tasks["c.default"] else {
            fatalError("No default task in c")
        }
        try test.assert(c_default_qualified["name"]?.string == "c_default")
    }
165
}