From 8e06a7f2d4d05e8408c9617e36d4503c898956cc Mon Sep 17 00:00:00 2001 From: thatguysimon Date: Tue, 3 Nov 2020 22:13:19 +0200 Subject: [PATCH] feat: display V3 provider states in HTML (#357) --- lib/pact/doc/interaction_view_model.rb | 12 +++++ lib/pact/doc/markdown/interaction.erb | 6 +-- lib/pact/doc/markdown/interaction_renderer.rb | 3 +- .../pact/doc/interaction_view_model_spec.rb | 46 +++++++++++++++++++ .../consumer_contract_renderer_spec.rb | 11 +++++ spec/support/markdown_pact_v3.json | 44 ++++++++++++++++++ 6 files changed, 118 insertions(+), 4 deletions(-) create mode 100644 spec/support/markdown_pact_v3.json diff --git a/lib/pact/doc/interaction_view_model.rb b/lib/pact/doc/interaction_view_model.rb index 5383b85e9..49e5b4dd8 100644 --- a/lib/pact/doc/interaction_view_model.rb +++ b/lib/pact/doc/interaction_view_model.rb @@ -8,6 +8,8 @@ class InteractionViewModel include Pact::ActiveSupportSupport + MARKDOWN_BOLD_CHARACTERS = '**' + def initialize interaction, consumer_contract @interaction = interaction @consumer_contract = consumer_contract @@ -52,6 +54,16 @@ def provider_state start_of_sentence = false markdown_escape apply_capitals(@interaction.provider_state.strip, start_of_sentence) end + def formatted_provider_states mark_bold: false + bold_marker = mark_bold ? MARKDOWN_BOLD_CHARACTERS : '' + + (@interaction.provider_states || []).map do |ps| + "#{bold_marker}" \ + "#{markdown_escape(apply_capitals(ps.name.strip, false))}" \ + "#{bold_marker}" + end.join(' and ') + end + def description start_of_sentence = false return '' unless @interaction.description markdown_escape apply_capitals(@interaction.description.strip, start_of_sentence) diff --git a/lib/pact/doc/markdown/interaction.erb b/lib/pact/doc/markdown/interaction.erb index ae2877a93..8aefd0cc5 100644 --- a/lib/pact/doc/markdown/interaction.erb +++ b/lib/pact/doc/markdown/interaction.erb @@ -1,6 +1,6 @@ - -<%= if interaction.has_provider_state? - "Given **#{h(interaction.provider_state)}**, upon receiving" +<% formatted_provider_states = h(interaction.formatted_provider_states mark_bold: true) %> +<%= if !formatted_provider_states.empty? + "Given #{formatted_provider_states}, upon receiving" else "Upon receiving" end diff --git a/lib/pact/doc/markdown/interaction_renderer.rb b/lib/pact/doc/markdown/interaction_renderer.rb index e721ba70a..cd66f4915 100644 --- a/lib/pact/doc/markdown/interaction_renderer.rb +++ b/lib/pact/doc/markdown/interaction_renderer.rb @@ -13,7 +13,8 @@ def initialize interaction, pact end def render_summary - suffix = interaction.has_provider_state? ? " given #{h(interaction.provider_state)}" : "" + formatted_provider_states = h(interaction.formatted_provider_states) + suffix = formatted_provider_states.empty? ? "" : " given #{formatted_provider_states}" "* [#{h(interaction.description(true))}](##{interaction.id})#{suffix}\n\n" end diff --git a/spec/lib/pact/doc/interaction_view_model_spec.rb b/spec/lib/pact/doc/interaction_view_model_spec.rb index 2cf3387b0..66438d2e3 100644 --- a/spec/lib/pact/doc/interaction_view_model_spec.rb +++ b/spec/lib/pact/doc/interaction_view_model_spec.rb @@ -181,6 +181,52 @@ module Doc end end end + + describe "formatted_provider_states" do + let(:consumer_contract) { Pact::ConsumerContract.from_uri './spec/support/markdown_pact.json' } + let(:interaction) { consumer_contract.interactions.first } + + context "when no provider state" do + let(:interaction) { consumer_contract.interactions.last } + + it "returns an empty string" do + expect(subject.formatted_provider_states).to eq "" + end + end + + context "when marking provider states in bold" do + it "formats the provider state in bold" do + expect(subject.formatted_provider_states mark_bold: true).to eq "**alligators exist**" + end + end + + context "when not marking provider states in bold" do + it "formats the provider state without bold" do + expect(subject.formatted_provider_states).to eq "alligators exist" + end + end + + context "when using v3 specification" do + let(:consumer_contract) { Pact::ConsumerContract.from_uri './spec/support/markdown_pact_v3.json' } + let(:interaction) { consumer_contract.interactions.first } + + context "when marking provider states in bold" do + it "formats the provider states in bold" do + expected_result = '**alligators exist** and **the city of Tel Aviv has a zoo** ' \ + 'and **the zoo keeps record of its alligator population**' + expect(subject.formatted_provider_states mark_bold: true).to eq expected_result + end + end + + context "when not marking provider states in bold" do + it "formats the provider states without bold" do + expected_result = 'alligators exist and the city of Tel Aviv has a zoo ' \ + 'and the zoo keeps record of its alligator population' + expect(subject.formatted_provider_states).to eq expected_result + end + end + end + end end end end diff --git a/spec/lib/pact/doc/markdown/consumer_contract_renderer_spec.rb b/spec/lib/pact/doc/markdown/consumer_contract_renderer_spec.rb index 00d930b33..a77d4c694 100644 --- a/spec/lib/pact/doc/markdown/consumer_contract_renderer_spec.rb +++ b/spec/lib/pact/doc/markdown/consumer_contract_renderer_spec.rb @@ -12,6 +12,17 @@ module Markdown subject { ConsumerContractRenderer.new(consumer_contract) } describe "#call" do + context "when using V3 specification" do + context "when an interaction has multiple provider states" do + let(:consumer_contract) { Pact::ConsumerContract.from_uri './spec/support/markdown_pact_v3.json' } + + it "displays all provider states in the interaction title" do + expect(subject.call).to include 'Given **alligators exist** and **the city of Tel Aviv has a zoo** ' \ + 'and **the zoo keeps record of its alligator population**, upon receiving' + end + end + end + context "with markdown characters in the pacticipant names" do let(:consumer_contract) { Pact::ConsumerContract.from_uri './spec/support/markdown_pact_with_markdown_chars_in_names.json' } diff --git a/spec/support/markdown_pact_v3.json b/spec/support/markdown_pact_v3.json new file mode 100644 index 000000000..2c28c6243 --- /dev/null +++ b/spec/support/markdown_pact_v3.json @@ -0,0 +1,44 @@ +{ + "provider": { + "name": "Some Provider" + }, + "consumer": { + "name": "Some Consumer" + }, + "interactions": [ + { + "description": "a request to list all alligators in Tel Aviv", + "providerStates": [ + {"name": "alligators exist", "params" : {}}, + {"name": "the city of Tel Aviv has a zoo", "params" : {}}, + {"name": "the zoo keeps record of its alligator population", "params" : {}} + ], + "params": {}, + "request": { + "method": "get", + "path": "/alligators" + }, + "response": { + "headers" : {"Content-Type": "application/json"}, + "status" : 200, + "body" : { + "alligators": [{ + "name": "Bob", + "phoneNumber" : { + "json_class": "Pact::Term", + "data": { + "generate": "12345678", + "matcher": {"json_class":"Regexp","o":0,"s":"\\d+"} + } + } + }] + } + } + } + ], + "metadata": { + "pactSpecification": { + "version": "3.0.0" + } + } +}