Skip to content

Commit

Permalink
Keep moving web specific code under Web module.
Browse files Browse the repository at this point in the history
  • Loading branch information
blambeau committed Mar 14, 2021
1 parent 2554d66 commit 122005e
Show file tree
Hide file tree
Showing 13 changed files with 91 additions and 74 deletions.
10 changes: 10 additions & 0 deletions lib/finitio/webspicy/shared.fio
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@import finitio/data

Tag = String( s | s.length > 0 )

Schema =
.Finitio::System <fio> String
\( s | ::Webspicy.schema(s) )
\( s | raise "Unsupported" )

Params = .Array|.Hash
73 changes: 21 additions & 52 deletions lib/webspicy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,10 @@ module Webspicy
require 'webspicy/specification'
require 'webspicy/configuration'
require 'webspicy/tester'
require 'webspicy/web'

class Error < StandardError; end
class TimeoutError < Error; end

###
### Backward compatibility
###
Client = Tester::Client
HttpClient = Web::HttpClient
RackTestClient = Web::RackTestClient
Resource = Specification
FileUpload = Specification::FileUpload
Scope = Configuration::Scope
Checker = Tester::FileChecker

###
### About folders
###
Expand All @@ -49,10 +37,11 @@ class TimeoutError < Error; end
### About formal doc and specifications defined there
###
Finitio.stdlib_path(Path.dir/"finitio")

DEFAULT_SYSTEM = Finitio.system(<<~FIO)
@import webspicy/scalars
FIO
FORMALDOC = Finitio.system(Path.dir/("webspicy/formaldoc.fio"))


###
### Exceptions that we let pass during testing
Expand All @@ -66,45 +55,6 @@ def default_scope
end
module_function :default_scope

def specification(raw, file = nil, scope = default_scope)
raw = YAML.load(raw) if raw.is_a?(String)
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, scope)
end
module_function :specification

def service(raw, scope = default_scope)
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 = default_scope)
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, scope)
# msg = "#{ex.message}:\n #{ex.root_cause.message}"
# msg = Support::Colorize.colorize_error(msg, scope.config)
# fatal(msg)
raise
end
module_function :handle_finitio_error

#
# Yields the block after having installed `scope` globally.
#
Expand Down Expand Up @@ -185,4 +135,23 @@ def fatal(*args, &bl)
end
module_function :fatal

require 'webspicy/web'

###
### Backward compatibility
###
Client = Tester::Client
HttpClient = Web::HttpClient
RackTestClient = Web::RackTestClient
Resource = Specification
FileUpload = Specification::FileUpload
Scope = Configuration::Scope
Checker = Tester::FileChecker

[:specification, :service, :test_case].each do |meth|
define_method(meth) do |*args, &bl|
Webspicy::Web.send(meth, *args, &bl)
end
module_function(meth)
end
end
2 changes: 2 additions & 0 deletions lib/webspicy/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def initialize(folder = Configuration.default_folder || Path.pwd, parent = nil)
:success => :green
}
@colorize = true
@factory = Webspicy::Web
@scope_factory = ->(config){ Scope.new(config) }
@client = Web::HttpClient
@reporter = default_reporter
Expand Down Expand Up @@ -109,6 +110,7 @@ def each_scope(&bl)
end
end

attr_accessor :factory
attr_accessor :scope_factory

def factor_scope
Expand Down
2 changes: 1 addition & 1 deletion lib/webspicy/configuration/scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def _each_specification_file(config, apply_filter = true)
def each_specification(apply_filter = true, &bl)
return enum_for(:each_specification, apply_filter) unless block_given?
each_specification_file(apply_filter) do |file, folder|
yield Webspicy.specification(file.load, file, self)
yield config.factory.specification(file.load, file, self)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/webspicy/configuration/single_url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def each_specification_file(*args, &bl)

def each_specification(*args, &bl)
return enum_for(:each_specification) unless block_given?
yield Webspicy.specification(specification_src, nil, self)
yield config.factory.specification(specification_src, nil, self)
end

def specification_src
Expand Down
2 changes: 1 addition & 1 deletion lib/webspicy/configuration/single_yml_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def each_specification_file(*args, &bl)

def each_specification(*args, &bl)
return enum_for(:each_specification) unless block_given?
yield Webspicy.specification(file.read, nil, self)
yield config.factory.specification(file.read, nil, self)
end

end # class SingleYmlFileScope
Expand Down
2 changes: 1 addition & 1 deletion lib/webspicy/specification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def self.singleservice(raw)
name: raw[:name] || "Unamed specification",
url: raw[:url],
services: [
Webspicy.service(raw.reject{|k| k==:url or k==:name }, Webspicy.current_scope)
Webspicy::Web.service(raw.reject{|k| k==:url or k==:name }, Webspicy.current_scope)
]
}
info(converted)
Expand Down
2 changes: 1 addition & 1 deletion lib/webspicy/specification/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def counterexamples
def generated_counterexamples
preconditions.map{|pre|
pre.counterexamples(self).map{|tc|
tc = Webspicy.test_case(tc, Webspicy.current_scope)
tc = config.factory.test_case(tc, Webspicy.current_scope)
tc.bind(self, true)
}
}.flatten
Expand Down
2 changes: 1 addition & 1 deletion lib/webspicy/tester.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def run_scope
def load_specification(spec_file)
@spec_file = spec_file
reporter.before_spec_file
Webspicy.specification(spec_file.load, spec_file, scope)
config.factory.specification(spec_file.load, spec_file, scope)
rescue *PASSTHROUGH_EXCEPTIONS
raise
rescue Exception => e
Expand Down
44 changes: 44 additions & 0 deletions lib/webspicy/web.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,47 @@
module Webspicy
module Web

FORMALDOC = Finitio.system(Path.dir/("web/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)
puts ex.root_cause.message
raise ex
end
module_function :handle_finitio_error

end # module Web
end # module Webspicy
require_relative 'web/client'
require_relative 'web/invocation'
require_relative 'web/mocker'
Expand Down
12 changes: 2 additions & 10 deletions lib/webspicy/formaldoc.fio → lib/webspicy/web/formaldoc.fio
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
@import finitio/data
@import webspicy/shared

Method =
String( s | s =~ /^(GET|POST|POST_FORM|PUT|DELETE|PATCH|PUT|OPTIONS)$/ )

Tag = String( s | s.length > 0 )

Schema =
.Finitio::System <fio> String
\( s | ::Webspicy.schema(s) )
\( s | raise "Unsupported" )

FileUpload =
.Webspicy::FileUpload <info> {
.Webspicy::Specification::FileUpload <info> {
path : String
content_type : String
param_name :? String
Expand Down Expand Up @@ -77,8 +71,6 @@ TestCase =
tags :? [Tag]
}

Params = .Array|.Hash

StatusRange = .Webspicy::Support::StatusRange
<int> Integer
<str> String(s | s =~ /^\dxx$/ )
8 changes: 4 additions & 4 deletions spec/unit/configuration/scope/test_expand_example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Configuration

context 'when the service has no default example' do
let(:service) {
Webspicy.service({
Webspicy::Web.service({
method: "GET",
description: "Test service",
preconditions: "Foo",
Expand All @@ -18,7 +18,7 @@ class Configuration
}

let(:example) {
Webspicy.test_case({
Webspicy::Web.test_case({
description: "Hello world"
})
}
Expand All @@ -30,7 +30,7 @@ class Configuration

context 'when the service has a default example' do
let(:service) {
Webspicy.service({
Webspicy::Web.service({
method: "GET",
description: "Test service",
preconditions: "Foo",
Expand All @@ -44,7 +44,7 @@ class Configuration
}

let(:example) {
Webspicy.test_case({
Webspicy::Web.test_case({
description: "Hello world",
expected: { content_type: "application/json" }
})
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/specification/service/test_dress_params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Specification
describe Service, "dress_params" do

it 'symbolizes keys' do
service = Webspicy.service({
service = Webspicy::Web.service({
method: "GET",
description: "Test service",
preconditions: "Foo",
Expand All @@ -17,7 +17,7 @@ class Specification
end

it 'supports an array' do
service = Webspicy.service({
service = Webspicy::Web.service({
method: "GET",
description: "Test service",
preconditions: "Foo",
Expand Down

0 comments on commit 122005e

Please sign in to comment.