Skip to content

Commit

Permalink
[WIP] Test the webspicy command through webspicy cmd layer.
Browse files Browse the repository at this point in the history
  • Loading branch information
blambeau authored and llambeau committed Nov 10, 2021
1 parent 93f8bd2 commit 2175bd4
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 4 deletions.
45 changes: 45 additions & 0 deletions lib/webspicy/cmd.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module Webspicy
module Cmd

require_relative 'cmd/specification'

FORMALDOC = Finitio.system(Path.dir/("cmd/formaldoc.fio"))

def specification(raw, file = nil, scope = Webspicy.default_scope)
raw = YAML.load(raw) if raw.is_a?(String)
Webspicy.with_scope(scope) do
r = FORMALDOC["Specification"].dress(raw)
r.config = scope.config
r.located_at!(file) if file
r
end
rescue Finitio::Error => ex
handle_finitio_error(ex)
end
module_function :specification

def service(raw, scope = Webspicy.default_scope)
Webspicy.with_scope(scope) do
FORMALDOC["Service"].dress(raw)
end
rescue Finitio::Error => ex
handle_finitio_error(ex)
end
module_function :service

def test_case(raw, scope = Webspicy.default_scope)
Webspicy.with_scope(scope) do
FORMALDOC["TestCase"].dress(raw)
end
rescue Finitio::Error => ex
handle_finitio_error(ex)
end
module_function :test_case

def handle_finitio_error(ex)
raise ex
end
module_function :handle_finitio_error

end # module Cmd
end # module Webspicy
37 changes: 37 additions & 0 deletions lib/webspicy/cmd/formaldoc.fio
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
@import finitio/data
@import webspicy/shared

Specification = .Webspicy::Cmd::Specification <singleservice> {
command : String
name :? String
description :? String
preconditions :? [String]|String
postconditions :? [String]|String
errconditions :? [String]|String
input_schema :? Schema
output_schema :? Schema
error_schema :? Schema
default_example :? TestCase
examples :? [TestCase]
counterexamples :? [TestCase]
}

Service = .Webspicy::Cmd::Specification::Service <info> {
description :? String
preconditions :? [String]|String
postconditions :? [String]|String
errconditions :? [String]|String
input_schema :? Schema
output_schema :? Schema
error_schema :? Schema
default_example :? TestCase
examples :? [TestCase]
counterexamples :? [TestCase]
}

TestCase = .Webspicy::Cmd::Specification::TestCase <info> {
description :? String
metadata :? { ...: .Object }
args :? [String]
assert :? { ...: .Object }
}
38 changes: 38 additions & 0 deletions lib/webspicy/cmd/specification.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module Webspicy
module Cmd
class Specification < Webspicy::Specification

class << self
def info(raw)
new(raw)
end

def singleservice(raw)
converted = {
name: raw[:name] || "Unamed specification",
command: raw[:command],
services: [
Webspicy::Cmd.service(raw.reject{|k| k==:command or k==:name }, Webspicy.current_scope)
]
}
info(converted)
rescue => ex
puts "Coucou: #{ex.message}"
puts ex.backtrace.join("\n")
raise
end
end

def command
@raw[:command]
end

def to_singleservice
raise NotImplementedError
end

end # class Specification
end # module Cmd
end # module Webspicy
require_relative 'specification/service'
require_relative 'specification/test_case'
8 changes: 8 additions & 0 deletions lib/webspicy/cmd/specification/service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Webspicy
module Cmd
class Specification
class Service < Webspicy::Specification::Service
end # class Service
end # class Specification
end # module Web
end # module Webspicy
8 changes: 8 additions & 0 deletions lib/webspicy/cmd/specification/test_case.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Webspicy
module Cmd
class Specification
class TestCase < Webspicy::Specification::TestCase
end # class TestCase
end # class Specification
end # module Web
end # module Webspicy
4 changes: 0 additions & 4 deletions lib/webspicy/specification/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,6 @@ def dress_params(params)
input_schema.dress(params)
end

def to_s
"#{method} #{specification.url}"
end

private

def compile_preconditions
Expand Down
4 changes: 4 additions & 0 deletions lib/webspicy/web/specification/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ def method
@raw[:method]
end

def to_s
"#{method} #{specification.url}"
end

end # class Service
end # class Specification
end # module Web
Expand Down
5 changes: 5 additions & 0 deletions spec/cmd/config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'webspicy/cmd'

Webspicy::Configuration.new(Path.dir) do |c|
c.factory = Webspicy::Cmd
end
25 changes: 25 additions & 0 deletions spec/cmd/formalspec/base.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
command:
webspicy {options} {args}

examples:

- description: |-
when called on a passing path
args:
- examples/website/get-http.yml
assert:
exit_code:
0
stdout: |-
>> specification/get-http.yml
GET http://yourbackendisbroken.dev, it works
v It has a 300 response status
v It has a `Content-Type: text/html` response header
v It has a `Location: https://yourbackendisbroken.dev/?id=1` response header
v Its output meets the expected data schema
1 spec file, 1 example, 0 counterexample
4 assertions, 0 error, 0 failure

0 comments on commit 2175bd4

Please sign in to comment.