Skip to content

Commit

Permalink
Add |-? operator and allow |-+ and |---+ to work with optionals (#28)
Browse files Browse the repository at this point in the history
* Add |-? operator.
Allow |-+ and |---+ to work with optionals.
Tests for conditionals and optionals.

* Removed |-+ and |---+ accepting optional in favour of
|-? and |---? which accept any optional instead of a conditional.
Expanded the |---? operators to work with Node + Node and [Node] + Node
to match the |---+ operators.

* Updated CI to Xcode 9.3

* Upgrade to Circle CI 2.0.
Minor formatting changes.

* Fighting with YAML.
  • Loading branch information
mluisbrown authored and andersio committed May 17, 2018
1 parent 6a23b6a commit a230d0f
Show file tree
Hide file tree
Showing 13 changed files with 286 additions and 16 deletions.
21 changes: 21 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
version: 2

jobs:
build-and-test:
macos:
xcode: "9.3.0"

working_directory: /Users/distiller/project

steps:
- checkout
- run: git submodule sync --recursive
- run: git submodule update --init --recursive
- run: script/test iphonesimulator "platform=iOS Simulator,name=iPhone 6,OS=11.3" Bento
- run: script/test iphonesimulator "platform=iOS Simulator,name=iPhone 6,OS=11.3" Example build

workflows:
version: 2
build-and-test:
jobs:
- build-and-test
8 changes: 8 additions & 0 deletions Bento.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
58FC444A207CFBD700DA3614 /* MovieComponentView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 58FC4440207CFBD700DA3614 /* MovieComponentView.xib */; };
58FC444D207CFBE200DA3614 /* BookAppointmentViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58FC444B207CFBE100DA3614 /* BookAppointmentViewController.swift */; };
58FC444E207CFBE200DA3614 /* MoviesListViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58FC444C207CFBE200DA3614 /* MoviesListViewController.swift */; };
740921B620ACDDDA00B59F5C /* IfTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 740921B520ACDDDA00B59F5C /* IfTests.swift */; };
740921B820ACE5EC00B59F5C /* ConcatenationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 740921B720ACE5EC00B59F5C /* ConcatenationTests.swift */; };
74208FA12083B1F00062CC8D /* Nimble.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 74208FA22083B1F00062CC8D /* Nimble.framework */; };
747523C72083A6660030CCAA /* FlexibleDiff.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 747523C82083A6660030CCAA /* FlexibleDiff.framework */; };
74C7FE982083A990005C8B53 /* Result.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 74C7FE972083A990005C8B53 /* Result.framework */; };
Expand Down Expand Up @@ -143,6 +145,8 @@
58FC4440207CFBD700DA3614 /* MovieComponentView.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = MovieComponentView.xib; sourceTree = "<group>"; };
58FC444B207CFBE100DA3614 /* BookAppointmentViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BookAppointmentViewController.swift; sourceTree = "<group>"; };
58FC444C207CFBE200DA3614 /* MoviesListViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MoviesListViewController.swift; sourceTree = "<group>"; };
740921B520ACDDDA00B59F5C /* IfTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IfTests.swift; sourceTree = "<group>"; };
740921B720ACE5EC00B59F5C /* ConcatenationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConcatenationTests.swift; sourceTree = "<group>"; };
74208FA22083B1F00062CC8D /* Nimble.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = Nimble.framework; sourceTree = BUILT_PRODUCTS_DIR; };
747523C82083A6660030CCAA /* FlexibleDiff.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = FlexibleDiff.framework; sourceTree = BUILT_PRODUCTS_DIR; };
74C7FE972083A990005C8B53 /* Result.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = Result.framework; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -310,6 +314,8 @@
9A3EF77F205D866F00D043AC /* AnyRenderableTests.swift */,
9A7846FC205EAF7C00FA597E /* NodeTests.swift */,
9A7846FE205EAF8400FA597E /* SectionTests.swift */,
740921B520ACDDDA00B59F5C /* IfTests.swift */,
740921B720ACE5EC00B59F5C /* ConcatenationTests.swift */,
9A784700205EB5E000FA597E /* TestRenderable.swift */,
9A784702205EB61D00FA597E /* TestId.swift */,
58FC4421207CF29F00DA3614 /* Info.plist */,
Expand Down Expand Up @@ -544,7 +550,9 @@
58FC442A207CF2BB00DA3614 /* NodeTests.swift in Sources */,
58FC4428207CF2BB00DA3614 /* TestId.swift in Sources */,
58FC4429207CF2BB00DA3614 /* TestRenderable.swift in Sources */,
740921B620ACDDDA00B59F5C /* IfTests.swift in Sources */,
58FC442B207CF2BB00DA3614 /* SectionTests.swift in Sources */,
740921B820ACE5EC00B59F5C /* ConcatenationTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
1 change: 1 addition & 0 deletions Bento/Bento/Box.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ precedencegroup SectionConcatenationPrecedence {
}

infix operator |-+: SectionConcatenationPrecedence
infix operator |-?: SectionConcatenationPrecedence
infix operator |---+: NodeConcatenationPrecedence
infix operator |---*: NodeConcatenationPrecedence
infix operator |---?: NodeConcatenationPrecedence
Expand Down
67 changes: 67 additions & 0 deletions Bento/Bento/If.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,24 @@ public struct If<T> {
return If(condition: condition, generator: generator)
}

public static func iff(_ condition: @autoclosure @escaping () -> Bool, _ generator: @escaping () -> T) -> If<T> {
return If(condition: condition, generator: generator)
}

public static func iff(_ condition: @escaping () -> Bool, _ generator: @escaping () -> T) -> If<T> {
return If(condition: condition, generator: generator)
}
}

public struct Some<T, U> {
let optional: T?
let generator: (T) -> U

public static func some(_ optional: T?, _ generator: @escaping (T) -> U) -> Some<T, U> {
return Some(optional: optional, generator: generator)
}
}

public func |---?<SectionId, RowId>(lhs: Section<SectionId, RowId>, rhs: If<Node<RowId>>) -> Section<SectionId, RowId> {
if rhs.condition() {
return lhs
Expand All @@ -26,3 +39,57 @@ public func |---?<SectionId, RowId>(lhs: Section<SectionId, RowId>, rhs: If<[Nod
}
return lhs
}

public func |---?<T, SectionId, RowId>(lhs: Section<SectionId, RowId>, rhs: Some<T, Node<RowId>>) -> Section<SectionId, RowId> {
return rhs.optional.map { value in
lhs |---+ rhs.generator(value)
} ?? lhs
}

public func |---?<T, SectionId, RowId>(lhs: Section<SectionId, RowId>, rhs: Some<T, [Node<RowId>]>) -> Section<SectionId, RowId> {
return rhs.optional.map { value in
lhs |---* rhs.generator(value)
} ?? lhs
}

public func |-?<SectionId, RowId>(lhs: Box<SectionId, RowId>, rhs: If<Section<SectionId, RowId>>) -> Box<SectionId, RowId> {
if rhs.condition() {
return lhs
|-+ rhs.generator()
}
return lhs
}

public func |-?<T, SectionId, RowId>(lhs: Box<SectionId, RowId>, rhs: Some<T, Section<SectionId, RowId>>) -> Box<SectionId, RowId> {
return rhs.optional.map { value in
lhs |-+ rhs.generator(value)
} ?? lhs
}

public func |---?<Identifier>(lhs: Node<Identifier>, rhs: If<Node<Identifier>>) -> [Node<Identifier>] {
if rhs.condition() {
return [lhs, rhs.generator()]
}

return [lhs]
}

public func |---?<Identifier>(lhs: [Node<Identifier>], rhs: If<Node<Identifier>>) -> [Node<Identifier>] {
if rhs.condition() {
return lhs + [rhs.generator()]
}

return lhs
}

public func |---?<T, Identifier>(lhs: Node<Identifier>, rhs: Some<T, Node<Identifier>>) -> [Node<Identifier>] {
return rhs.optional.map { value in
[lhs, rhs.generator(value)]
} ?? [lhs]
}

public func |---?<T, Identifier>(lhs: [Node<Identifier>], rhs: Some<T, Node<Identifier>>) -> [Node<Identifier>] {
return rhs.optional.map { value in
lhs + [rhs.generator(value)]
} ?? lhs
}
2 changes: 1 addition & 1 deletion Bento/Diff/TableViewSectionDiff.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ struct TableViewSectionDiff<SectionId: Hashable, RowId: Hashable> {
tableView.insertRows(at: sectionMutation.insertedIndexPaths, with: animation.rowInsertion)
tableView.perform(moves: sectionMutation.movedIndexPaths)
[sectionMutation.changeset.moves.lazy
.flatMap { $0.isMutated ? ($0.source, $0.destination) : nil },
.compactMap { $0.isMutated ? ($0.source, $0.destination) : nil },
sectionMutation.changeset.mutations.lazy.map { ($0, $0) }]
.joined()
.forEach { source, destination in
Expand Down
79 changes: 79 additions & 0 deletions BentoTests/ConcatenationTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import Nimble
import XCTest
import UIKit
@testable import Bento

class ConcatenationTests: XCTestCase {

func testSectionConcatentation() {
let section = Section<TestSectionId, TestRowId>(id: .first)

let box = Box<TestSectionId, TestRowId>.empty
|-+ section

expect(box.sections.count) == 1
}

func testSectionOptionalNilConcatentation() {
let section = Section<TestSectionId, TestRowId>(id: .first)
let optional: String? = nil

let box = Box<TestSectionId, TestRowId>.empty
|-? .some(optional) { _ in
section
}

expect(box.sections.count) == 0
}

func testSectionOptionalSomeConcatentation() {
let section = Section<TestSectionId, TestRowId>(id: .first)
let optional: String? = "something"

let box = Box<TestSectionId, TestRowId>.empty
|-? .some(optional) { _ in
section
}

expect(box.sections.count) == 1
}

func testNodeConcatenation() {
let section = Section<TestSectionId, TestRowId>(id: .first)
let node = Node(id: TestRowId.first,
component: TestCustomEqualityRenderable(value: 0))

let result = section
|---+ node

expect(result.rows.count) == 1
}

func testNodeOptionalNilConcatenation() {
let section = Section<TestSectionId, TestRowId>(id: .first)
let node = Node(id: TestRowId.first,
component: TestCustomEqualityRenderable(value: 0))
let optional: String? = nil

let result: Section<TestSectionId, TestRowId> = section
|---? .some(optional) { _ in
node
}

expect(result.rows.count) == 0
}

func testNodeOptionalSomeConcatenation() {
let section = Section<TestSectionId, TestRowId>(id: .first)
let node = Node(id: TestRowId.first,
component: TestCustomEqualityRenderable(value: 0))
let optional: String? = "something"

let result: Section<TestSectionId, TestRowId> = section
|---? .some(optional) { _ in
node
}

expect(result.rows.count) == 1
}
}
95 changes: 95 additions & 0 deletions BentoTests/IfTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import Nimble
import XCTest
import UIKit
@testable import Bento

class IfTests: XCTestCase {

func testSectionConditionalTrueConcatentation() {
let section = Section<TestSectionId, TestRowId>(id: .first)

let box = Box<TestSectionId, TestRowId>.empty
|-? .iff(true, section)

expect(box.sections.count) == 1
}

func testSectionConditionalTrueClosureConcatentation() {
let section = Section<TestSectionId, TestRowId>(id: .first)

let box = Box<TestSectionId, TestRowId>.empty
|-? .iff(true) {
section
}

expect(box.sections.count) == 1
}

func testSectionConditionalFalseConcatentation() {
let section = Section<TestSectionId, TestRowId>(id: .first)

let box = Box<TestSectionId, TestRowId>.empty
|-? .iff(false, section)

expect(box.sections.count) == 0
}

func testSectionConditionalFalseClosureConcatentation() {
let section = Section<TestSectionId, TestRowId>(id: .first)

let box = Box<TestSectionId, TestRowId>.empty
|-? .iff(false) {
section
}

expect(box.sections.count) == 0
}

func testNodeConditionalTrueConcatenation() {
let section = Section<TestSectionId, TestRowId>(id: .first)
let node = Node(id: TestRowId.first,
component: TestCustomEqualityRenderable(value: 0))

let result = section
|---? .iff(true, node)

expect(result.rows.count) == 1
}

func testNodeConditionalTrueClosureConcatenation() {
let section = Section<TestSectionId, TestRowId>(id: .first)
let node = Node(id: TestRowId.first,
component: TestCustomEqualityRenderable(value: 0))

let result = section
|---? .iff(true) {
node
}

expect(result.rows.count) == 1
}

func testNodeConditionalFalseConcatenation() {
let section = Section<TestSectionId, TestRowId>(id: .first)
let node = Node(id: TestRowId.first,
component: TestCustomEqualityRenderable(value: 0))

let result = section
|---? .iff(false, node)

expect(result.rows.count) == 0
}

func testNodeConditionalFalseClosureConcatenation() {
let section = Section<TestSectionId, TestRowId>(id: .first)
let node = Node(id: TestRowId.first,
component: TestCustomEqualityRenderable(value: 0))

let result = section
|---? .iff(false) {
node
}

expect(result.rows.count) == 0
}
}
6 changes: 3 additions & 3 deletions Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
github "Babylonpartners/ReactiveFeedback" "0.2.0"
github "Quick/Nimble" "v7.1.0"
github "Quick/Nimble" "v7.1.1"
github "RACCommunity/FlexibleDiff" "0.0.5"
github "ReactiveCocoa/ReactiveCocoa" "7.2.0"
github "ReactiveCocoa/ReactiveSwift" "3.1.0"
github "antitypical/Result" "3.2.4"
github "jspahrsummers/xcconfigs" "0.11"
github "onevcat/Kingfisher" "4.7.0"
github "jspahrsummers/xcconfigs" "0.12"
github "onevcat/Kingfisher" "4.8.0"
2 changes: 1 addition & 1 deletion Carthage/Checkouts/Kingfisher
Submodule Kingfisher updated 175 files
2 changes: 1 addition & 1 deletion Carthage/Checkouts/xcconfigs
9 changes: 0 additions & 9 deletions circle.yml

This file was deleted.

0 comments on commit a230d0f

Please sign in to comment.