CommandParamsTests.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //
  2. // CommandParamsTests.swift
  3. //
  4. // Created by jason on 30/8/23.
  5. //
  6. import Quick
  7. import Nimble
  8. @testable import CutBoxCLICore
  9. import Foundation
  10. class CommandParamsSpec: QuickSpec {
  11. override func spec() {
  12. var subject: CommandParams!
  13. beforeEach {
  14. subject = CommandParams(out: MockOutput(), arguments: [])
  15. }
  16. describe("hasOption function") {
  17. it("should return the specified value for String type") {
  18. subject.arguments = ["-opt", "stringValue"]
  19. let result: String? = subject.hasOption("-opt")
  20. expect(result).to(equal("stringValue"))
  21. }
  22. it("should return the specified value for Int type") {
  23. subject.arguments = ["-opt", "42"]
  24. let result: Int? = subject.hasOption("-opt")
  25. expect(result).to(equal(42))
  26. }
  27. it("should return the specified value for Double type") {
  28. subject.arguments = ["-opt", "3.14"]
  29. let result: Double? = subject.hasOption("-opt")
  30. expect(result).to(beCloseTo(3.14))
  31. }
  32. it("should return nil if option is not present") {
  33. subject.arguments = ["--otherOpt", "value"]
  34. let result: String? = subject.hasOption("--opt")
  35. expect(result).to(beNil())
  36. }
  37. it("should return nil if option value starts with '--'") {
  38. subject.arguments = ["--opt", "--invalidValue"]
  39. let result: String? = subject.hasOption("--opt")
  40. expect(result).to(beNil())
  41. }
  42. it("will throw a fatalError if the return type is not String, Int or Double") {
  43. subject.arguments = ["--opt", ""]
  44. expect { subject.hasOption("--opt") }.to(throwAssertion())
  45. }
  46. }
  47. describe("timeOption") {
  48. it("reads a time option and checks existing arguments") {
  49. subject.arguments = ["--since", "1 day"]
  50. let time = Date().timeIntervalSince1970 - 86400.0
  51. let result = subject.timeOption("--since")
  52. expect(result).to(beCloseTo(time, within: 1.5))
  53. }
  54. it("returns nil for an invalid time value") {
  55. subject.arguments = ["--since", "Oi Billy"]
  56. let result = subject.timeOption("--since")
  57. expect(result).to(beNil())
  58. }
  59. }
  60. describe("parseSeconds") {
  61. context("tiny time language") {
  62. for unit in [
  63. ("s", 1.0),
  64. ("sec", 1.0),
  65. ("secs", 1.0),
  66. ("m", 60.0),
  67. ("min", 60.0),
  68. ("mins", 60.0),
  69. ("h", 3600.0),
  70. ("hr", 3600.0),
  71. ("hrs", 3600.0),
  72. ("d", 86400.0),
  73. ("day", 86400.0),
  74. ("days", 86400.0),
  75. ("w", 604800.0),
  76. ("wk", 604800.0),
  77. ("week", 604800.0),
  78. ("weeks", 604800.0)
  79. ] {
  80. for value in [
  81. 1.0,
  82. 3.0,
  83. 5.0,
  84. 7.0
  85. ] {
  86. it("parses \(Int(value)) \(unit.0)") {
  87. expect(subject.parseToSeconds("\(Int(value)) \(unit.0)")) == unit.1 * value
  88. }
  89. }
  90. }
  91. }
  92. }
  93. describe("collectErrors") {
  94. context("after parsing, arguments left over are errors") {
  95. it("collects remaining arguments") {
  96. let arguments = ["-o0", "value1",
  97. "-o1", "value2",
  98. "--opt3",
  99. "--opt4", "value2"]
  100. subject.collectErrors(arguments)
  101. expect(subject.errors.count) == 4
  102. expect(subject.errors[0]) == ("-o0", "value1")
  103. expect(subject.errors[1]) == ("-o1", "value2")
  104. expect(subject.errors[2]) == ("--opt3", "")
  105. expect(subject.errors[3]) == ("--opt4", "value2")
  106. }
  107. it("concatenates consecutive values for an option") {
  108. let arguments = ["--something-invalid", "1", "day"]
  109. subject.collectErrors(arguments)
  110. expect(subject.errors.count) == 1
  111. expect(subject.errors[0]) == ("--something-invalid", "1 day")
  112. }
  113. }
  114. }
  115. }
  116. }