Skip to content

Commit

Permalink
Merge pull request #86 from groue/feature/swiftpm
Browse files Browse the repository at this point in the history
Run tests on Swift Package Manager
  • Loading branch information
fumito-ito authored Sep 22, 2022
2 parents 1a61ab6 + b771492 commit edbe65d
Show file tree
Hide file tree
Showing 20 changed files with 146 additions and 732 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/check-pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: check-pr

on: [push]

jobs:
build:
runs-on: macos-latest

steps:
- uses: actions/checkout@v2
with:
submodules: true

- name: Build
run: swift build

- name: Test
run: swift test
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.swiftpm

## https://raw.githubusercontent.com/github/gitignore/master/Global/OSX.gitignore

.DS_Store
Expand Down
25 changes: 23 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
// swift-tools-version:5.0
// swift-tools-version:5.3

import PackageDescription

let package = Package(
name: "Mustache",
platforms: [
.iOS(.v8),
.macOS(.v10_10),
.tvOS(.v9)
],
products: [
.library(
name: "Mustache",
Expand All @@ -14,6 +19,22 @@ let package = Package(
.target(
name: "Mustache",
dependencies: [],
path: "Sources")
path: "Sources"
),
.testTarget(
name: "MustacheTests",
dependencies: [
"Mustache"
],
path: "Tests",
exclude: [
"Carthage"
],
resources: [
.process("Public/ServicesTests/LocalizerTestsBundle"), // localized strings
.process("vendor"), // mustache specs
.process("Public/SuitesTests/twitter/HoganSuite") // Hogan specs
]
)
]
)
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class MustacheRenderableGuideTests: XCTestCase {
}
}
let render = { (info: RenderingInfo) -> Rendering in
let template = try! Template(named: "Person", bundle: Bundle(for: MustacheRenderableGuideTests.self))
let template = try! Template(named: "Person", bundle: Bundle.ofFileDirectory(filePath: #file))
let context = info.context.extendedContext(self)
return try template.render(context)
}
Expand All @@ -149,7 +149,7 @@ class MustacheRenderableGuideTests: XCTestCase {
}
}
let render = { (info: RenderingInfo) -> Rendering in
let template = try! Template(named: "Movie", bundle: Bundle(for: MustacheRenderableGuideTests.self))
let template = try! Template(named: "Movie", bundle: Bundle.ofFileDirectory(filePath: #file))
let context = info.context.extendedContext(self)
return try template.render(context)
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/Public/DocumentationTests/ReadMeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class ReadMeTests: XCTestCase {
}

func testReadmeExample1() {
let testBundle = Bundle(for: type(of: self))
let testBundle = Bundle.ofFileDirectory(filePath: #file)
let template = try! Template(named: "ReadMeExample1", bundle: testBundle)
let data: [String: Any] = [
"name": "Chris",
Expand Down Expand Up @@ -88,7 +88,7 @@ class ReadMeTests: XCTestCase {

// I have 3 cats.

let testBundle = Bundle(for: type(of: self))
let testBundle = Bundle.ofFileDirectory(filePath: #file)
let template = try! Template(named: "ReadMeExample2", bundle: testBundle)
let data = ["cats": ["Kitty", "Pussy", "Melba"]]
let rendering = try! template.render(data)
Expand Down
2 changes: 1 addition & 1 deletion Tests/Public/ServicesTests/LocalizerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import Mustache

class LocalizerTests: XCTestCase {

lazy var localizableBundle: Bundle = Bundle(path: Bundle(for: type(of: self)).path(forResource: "LocalizerTestsBundle", ofType: nil)!)!
lazy var localizableBundle: Bundle = Bundle.module
lazy var localizer: StandardLibrary.Localizer = StandardLibrary.Localizer(bundle: self.localizableBundle, table: nil)

func testLocalizableBundle() {
Expand Down

This file was deleted.

56 changes: 28 additions & 28 deletions Tests/Public/SuitesTests/GRMustacheSpec/GRMustacheSpecTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,43 +28,43 @@ class GRMustacheSpecTests: SuiteTestCase {

func testSuite() {
// General
runTestsFromResource("comments.json", directory: "Tests/general")
runTestsFromResource("delimiters.json", directory: "Tests/general")
runTestsFromResource("general.json", directory: "Tests/general")
runTestsFromResource("partials.json", directory: "Tests/general")
runTestsFromResource("pragmas.json", directory: "Tests/general")
runTestsFromResource("sections.json", directory: "Tests/general")
runTestsFromResource("inverted_sections.json", directory: "Tests/general")
runTestsFromResource("text_rendering.json", directory: "Tests/general")
runTestsFromResource("variables.json", directory: "Tests/general")
runTestsFromResource("comments", ofType: "json")
runTestsFromResource("delimiters", ofType: "json")
runTestsFromResource("general", ofType: "json")
runTestsFromResource("partials", ofType: "json")
runTestsFromResource("pragmas", ofType: "json")
runTestsFromResource("sections", ofType: "json")
runTestsFromResource("inverted_sections", ofType: "json")
runTestsFromResource("text_rendering", ofType: "json")
runTestsFromResource("variables", ofType: "json")

// Errors
runTestsFromResource("expression_parsing_errors.json", directory: "Tests/errors")
runTestsFromResource("tag_parsing_errors.json", directory: "Tests/errors")
runTestsFromResource("expression_parsing_errors", ofType: "json")
runTestsFromResource("tag_parsing_errors", ofType: "json")

// Expressions
runTestsFromResource("compound_keys.json", directory: "Tests/expressions")
runTestsFromResource("filters.json", directory: "Tests/expressions")
runTestsFromResource("implicit_iterator.json", directory: "Tests/expressions")
runTestsFromResource("compound_keys", ofType: "json")
runTestsFromResource("filters", ofType: "json")
runTestsFromResource("implicit_iterator", ofType: "json")

// Inheritance
runTestsFromResource("blocks.json", directory: "Tests/inheritance")
runTestsFromResource("partial_overrides.json", directory: "Tests/inheritance")
runTestsFromResource("blocks", ofType: "json")
runTestsFromResource("partial_overrides", ofType: "json")

// Standard library
runTestsFromResource("each.json", directory: "Tests/standard_library")
runTestsFromResource("HTMLEscape.json", directory: "Tests/standard_library")
runTestsFromResource("javascriptEscape.json", directory: "Tests/standard_library")
runTestsFromResource("URLEscape.json", directory: "Tests/standard_library")
runTestsFromResource("zip.json", directory: "Tests/standard_library")
runTestsFromResource("each", ofType: "json")
runTestsFromResource("HTMLEscape", ofType: "json")
runTestsFromResource("javascriptEscape", ofType: "json")
runTestsFromResource("URLEscape", ofType: "json")
runTestsFromResource("zip", ofType: "json")

// Values
runTestsFromResource("array.json", directory: "Tests/values")
runTestsFromResource("bool.json", directory: "Tests/values")
runTestsFromResource("dictionary.json", directory: "Tests/values")
runTestsFromResource("missing_value.json", directory: "Tests/values")
runTestsFromResource("null.json", directory: "Tests/values")
runTestsFromResource("number.json", directory: "Tests/values")
runTestsFromResource("string.json", directory: "Tests/values")
runTestsFromResource("array", ofType: "json")
runTestsFromResource("bool", ofType: "json")
runTestsFromResource("dictionary", ofType: "json")
runTestsFromResource("missing_value", ofType: "json")
runTestsFromResource("null", ofType: "json")
runTestsFromResource("number", ofType: "json")
runTestsFromResource("string", ofType: "json")
}
}
18 changes: 9 additions & 9 deletions Tests/Public/SuitesTests/SuiteTestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,27 @@ import XCTest
import Mustache

class SuiteTestCase: XCTestCase {
func runTestsFromResource(_ name: String, directory: String) {
let testBundle = Bundle(for: type(of: self))
guard let path = testBundle.path(forResource: name, ofType: nil, inDirectory: directory) else {
XCTFail("No such test suite \(directory)/\(name)")

func runTestsFromResource(_ name: String, ofType: String) {
let testBundle = Bundle.module
guard let path = testBundle.path(forResource: name, ofType: ofType) else {
XCTFail("No such test suite \(name)")
return
}

let data: Data! = try? Data(contentsOf: URL(fileURLWithPath: path))
if data == nil {
XCTFail("No test suite in \(path)")
return
}

let testSuite = try! JSONSerialization.jsonObject(with: data, options:JSONSerialization.ReadingOptions(rawValue: 0)) as! NSDictionary

guard let tests = testSuite["tests"] as? NSArray else {
XCTFail("Missing tests in \(path)")
return
}

for testDictionary in tests {
let test = Test(path: path, dictionary: testDictionary as! NSDictionary)
test.run()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class HoganSuite: SuiteTestCase {
func testSuite() {
// This suite contains template inheritance tests taken from
// https://github.com/twitter/hogan.js/blob/master/test/index.js
runTestsFromResource("template_inheritance.json", directory: "HoganSuite")
runTestsFromResource("template_inheritance", ofType: "json")
}

func testLambdaExpressionInInheritedTemplateSubsections() {
Expand Down
12 changes: 6 additions & 6 deletions Tests/Public/TagTests/TagTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class TagTests: XCTestCase {
}

tagDescription = nil
let bundle = Bundle(for: type(of: self))
let bundle = Bundle.ofFileDirectory(filePath: #file)!
let templateRepository = TemplateRepository(bundle: bundle)
var template = try! templateRepository.template(named: "TagTests")
template.baseContext = template.baseContext.extendedContext(willRender)
Expand All @@ -116,7 +116,7 @@ class TagTests: XCTestCase {
}

tagDescription = nil
let bundle = Bundle(for: type(of: self))
let bundle = Bundle.ofFileDirectory(filePath: #file)!
let templateRepository = TemplateRepository(baseURL: bundle.resourceURL!)
var template = try! templateRepository.template(named: "TagTests")
template.baseContext = template.baseContext.extendedContext(willRender)
Expand All @@ -140,7 +140,7 @@ class TagTests: XCTestCase {
}

tagDescription = nil
let bundle = Bundle(for: type(of: self))
let bundle = Bundle.ofFileDirectory(filePath: #file)!
let templateRepository = TemplateRepository(directoryPath: bundle.resourcePath!)
var template = try! templateRepository.template(named: "TagTests")
template.baseContext = template.baseContext.extendedContext(willRender)
Expand All @@ -164,7 +164,7 @@ class TagTests: XCTestCase {
}

tagDescription = nil
let bundle = Bundle(for: type(of: self))
let bundle = Bundle.ofFileDirectory(filePath: #file)!
let templateRepository = TemplateRepository(bundle: bundle)
var template = try! templateRepository.template(named: "TagTests_wrapper")
template.baseContext = template.baseContext.extendedContext(willRender)
Expand Down Expand Up @@ -195,7 +195,7 @@ class TagTests: XCTestCase {
}

tagDescription = nil
let bundle = Bundle(for: type(of: self))
let bundle = Bundle.ofFileDirectory(filePath: #file)!
let templateRepository = TemplateRepository(baseURL: bundle.resourceURL!)
var template = try! templateRepository.template(named: "TagTests_wrapper")
template.baseContext = template.baseContext.extendedContext(willRender)
Expand Down Expand Up @@ -226,7 +226,7 @@ class TagTests: XCTestCase {
}

tagDescription = nil
let bundle = Bundle(for: type(of: self))
let bundle = Bundle.ofFileDirectory(filePath: #file)!
let templateRepository = TemplateRepository(directoryPath: bundle.resourcePath!)
var template = try! templateRepository.template(named: "TagTests_wrapper")
template.baseContext = template.baseContext.extendedContext(willRender)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import Mustache
class TemplateRepositoryBundleTests: XCTestCase {

func testTemplateRepositoryWithBundle() {
let repo = TemplateRepository(bundle: Bundle(for: type(of: self)))
let repo = TemplateRepository(bundle: Bundle.ofFileDirectory(filePath: #file)!)
var template: Template
var rendering: String

Expand All @@ -51,7 +51,7 @@ class TemplateRepositoryBundleTests: XCTestCase {
}

func testTemplateRepositoryWithBundleTemplateExtensionEncoding() {
var repo = TemplateRepository(bundle: Bundle(for: type(of: self)), templateExtension: "text", encoding: String.Encoding.utf8)
var repo = TemplateRepository(bundle: Bundle.ofFileDirectory(filePath: #file)!, templateExtension: "text", encoding: String.Encoding.utf8)
var template: Template
var rendering: String

Expand All @@ -69,7 +69,7 @@ class TemplateRepositoryBundleTests: XCTestCase {
rendering = try! template.render()
XCTAssertEqual(rendering, "TemplateRepositoryBundleTests.text TemplateRepositoryBundleTests_partial.text")

repo = TemplateRepository(bundle: Bundle(for: type(of: self)), templateExtension: "", encoding: String.Encoding.utf8)
repo = TemplateRepository(bundle: Bundle.ofFileDirectory(filePath: #file), templateExtension: "", encoding: String.Encoding.utf8)

do {
_ = try repo.template(named: "notFound")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import Mustache
class TemplateRepositoryPathTests: XCTestCase {

func testTemplateRepositoryWithURL() {
let testBundle = Bundle(for: type(of: self))
let testBundle = Bundle.ofFileDirectory(filePath: #file)!
let directoryPath = testBundle.path(forResource: "TemplateRepositoryFileSystemTests_UTF8", ofType: nil)!
let repo = TemplateRepository(directoryPath: directoryPath)
var template: Template
Expand Down Expand Up @@ -57,7 +57,7 @@ class TemplateRepositoryPathTests: XCTestCase {
}

func testTemplateRepositoryWithURLTemplateExtensionEncoding() {
let testBundle = Bundle(for: type(of: self))
let testBundle = Bundle.ofFileDirectory(filePath: #file)!
var directoryPath: String
var repo: TemplateRepository
var template: Template
Expand Down Expand Up @@ -101,7 +101,7 @@ class TemplateRepositoryPathTests: XCTestCase {
}

func testAbsolutePartialName() {
let testBundle = Bundle(for: type(of: self))
let testBundle = Bundle.ofFileDirectory(filePath: #file)!
let directoryPath = testBundle.path(forResource: "TemplateRepositoryFileSystemTests", ofType: nil)!
let repo = TemplateRepository(directoryPath: directoryPath)
let template = try! repo.template(named: "base")
Expand All @@ -110,7 +110,7 @@ class TemplateRepositoryPathTests: XCTestCase {
}

func testPartialNameCanNotEscapeTemplateRepositoryRootDirectory() {
let testBundle = Bundle(for: type(of: self))
let testBundle = Bundle.ofFileDirectory(filePath: #file)!
let directoryPath = testBundle.path(forResource: "TemplateRepositoryFileSystemTests", ofType: nil)!
let repo = TemplateRepository(directoryPath: (directoryPath as NSString).appendingPathComponent("partials"))

Expand All @@ -129,7 +129,7 @@ class TemplateRepositoryPathTests: XCTestCase {
}

func testFileNameContainsWhitespace() {
let testBundle = Bundle(for: type(of: self))
let testBundle = Bundle.ofFileDirectory(filePath: #file)!
let directoryPath = testBundle.path(forResource: "TemplateRepositoryFileSystemTests", ofType: nil)!
let repo = TemplateRepository(directoryPath: directoryPath)
let template = try! repo.template(named: "contains whitespace")
Expand All @@ -138,7 +138,7 @@ class TemplateRepositoryPathTests: XCTestCase {
}

func testFileAndDictonaryNameContainsWhitespace() {
let testBundle = Bundle(for: type(of: self))
let testBundle = Bundle.ofFileDirectory(filePath: #file)!
let directoryPath = testBundle.path(forResource: "TemplateRepositoryFileSystemTests/contains whitespace", ofType: nil)!
let repo = TemplateRepository(directoryPath: directoryPath)
let template = try! repo.template(named: "contains whitespace")
Expand Down
Loading

0 comments on commit edbe65d

Please sign in to comment.