You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

json_spec.rb 30 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. # frozen_string_literal: true
  2. require 'spec_helper'
  3. require 'cucumber/formatter/spec_helper'
  4. require 'cucumber/formatter/json'
  5. require 'cucumber/cli/options'
  6. require 'json'
  7. module Cucumber
  8. module Formatter
  9. describe Json do
  10. extend SpecHelperDsl
  11. include SpecHelper
  12. context 'Given a single feature' do
  13. before(:each) do
  14. @out = StringIO.new
  15. @formatter = Json.new(actual_runtime.configuration.with_options(out_stream: @out))
  16. run_defined_feature
  17. end
  18. describe 'with a scenario with an undefined step' do
  19. define_feature <<-FEATURE
  20. Feature: Banana party
  21. Scenario: Monkey eats bananas
  22. Given there are bananas
  23. FEATURE
  24. it 'outputs the json data' do
  25. expect(load_normalised_json(@out)).to eq JSON.parse(%(
  26. [{"id": "banana-party",
  27. "uri": "spec.feature",
  28. "keyword": "Feature",
  29. "name": "Banana party",
  30. "line": 1,
  31. "description": "",
  32. "elements":
  33. [{"id": "banana-party;monkey-eats-bananas",
  34. "keyword": "Scenario",
  35. "name": "Monkey eats bananas",
  36. "line": 3,
  37. "description": "",
  38. "type": "scenario",
  39. "steps":
  40. [{"keyword": "Given ",
  41. "name": "there are bananas",
  42. "line": 4,
  43. "match": {"location": "spec.feature:4"},
  44. "result": {"status": "undefined"}}]}]}]))
  45. end
  46. end
  47. describe 'with a scenario with a passed step' do
  48. define_feature <<-FEATURE
  49. Feature: Banana party
  50. Scenario: Monkey eats bananas
  51. Given there are bananas
  52. FEATURE
  53. define_steps do
  54. Given(/^there are bananas$/) {}
  55. end
  56. it 'outputs the json data' do
  57. expect(load_normalised_json(@out)).to eq JSON.parse(%(
  58. [{"id": "banana-party",
  59. "uri": "spec.feature",
  60. "keyword": "Feature",
  61. "name": "Banana party",
  62. "line": 1,
  63. "description": "",
  64. "elements":
  65. [{"id": "banana-party;monkey-eats-bananas",
  66. "keyword": "Scenario",
  67. "name": "Monkey eats bananas",
  68. "line": 3,
  69. "description": "",
  70. "type": "scenario",
  71. "steps":
  72. [{"keyword": "Given ",
  73. "name": "there are bananas",
  74. "line": 4,
  75. "match": {"location": "spec/cucumber/formatter/json_spec.rb:63"},
  76. "result": {"status": "passed",
  77. "duration": 1}}]}]}]))
  78. end
  79. end
  80. describe 'with a scenario with a failed step' do
  81. define_feature <<-FEATURE
  82. Feature: Banana party
  83. Scenario: Monkey eats bananas
  84. Given there are bananas
  85. FEATURE
  86. define_steps do
  87. Given(/^there are bananas$/) { raise 'no bananas' }
  88. end
  89. it 'outputs the json data' do
  90. expect(load_normalised_json(@out)).to eq JSON.parse(%{
  91. [{"id": "banana-party",
  92. "uri": "spec.feature",
  93. "keyword": "Feature",
  94. "name": "Banana party",
  95. "line": 1,
  96. "description": "",
  97. "elements":
  98. [{"id": "banana-party;monkey-eats-bananas",
  99. "keyword": "Scenario",
  100. "name": "Monkey eats bananas",
  101. "line": 3,
  102. "description": "",
  103. "type": "scenario",
  104. "steps":
  105. [{"keyword": "Given ",
  106. "name": "there are bananas",
  107. "line": 4,
  108. "match": {"location": "spec/cucumber/formatter/json_spec.rb:100"},
  109. "result": {"status": "failed",
  110. "error_message": "no bananas (RuntimeError)\\n./spec/cucumber/formatter/json_spec.rb:100:in `/^there are bananas$/'\\nspec.feature:4:in `there are bananas'",
  111. "duration": 1}}]}]}]})
  112. end
  113. end
  114. describe 'with a scenario with a pending step' do
  115. define_feature <<-FEATURE
  116. Feature: Banana party
  117. Scenario: Monkey eats bananas
  118. Given there are bananas
  119. FEATURE
  120. define_steps do
  121. Given(/^there are bananas$/) { pending }
  122. end
  123. it 'outputs the json data' do
  124. expect(load_normalised_json(@out)).to eq JSON.parse(%{
  125. [{"id": "banana-party",
  126. "uri": "spec.feature",
  127. "keyword": "Feature",
  128. "name": "Banana party",
  129. "line": 1,
  130. "description": "",
  131. "elements":
  132. [{"id": "banana-party;monkey-eats-bananas",
  133. "keyword": "Scenario",
  134. "name": "Monkey eats bananas",
  135. "line": 3,
  136. "description": "",
  137. "type": "scenario",
  138. "steps":
  139. [{"keyword": "Given ",
  140. "name": "there are bananas",
  141. "line": 4,
  142. "match": {"location": "spec/cucumber/formatter/json_spec.rb:138"},
  143. "result": {"status": "pending",
  144. "error_message": "TODO (Cucumber::Pending)\\n./spec/cucumber/formatter/json_spec.rb:138:in `/^there are bananas$/'\\nspec.feature:4:in `there are bananas'",
  145. "duration": 1}}]}]}]})
  146. end
  147. end
  148. describe 'with a scenario outline with one example' do
  149. define_feature <<-FEATURE
  150. Feature: Banana party
  151. Scenario Outline: Monkey eats <fruit>
  152. Given there are <fruit>
  153. Examples: Fruit Table
  154. | fruit |
  155. | bananas |
  156. FEATURE
  157. define_steps do
  158. Given(/^there are bananas$/) {}
  159. end
  160. it 'outputs the json data' do
  161. expect(load_normalised_json(@out)).to eq JSON.parse(%(
  162. [{"id": "banana-party",
  163. "uri": "spec.feature",
  164. "keyword": "Feature",
  165. "name": "Banana party",
  166. "line": 1,
  167. "description": "",
  168. "elements":
  169. [{"id": "banana-party;monkey-eats-<fruit>;fruit-table;2",
  170. "keyword": "Scenario Outline",
  171. "name": "Monkey eats bananas",
  172. "line": 8,
  173. "description": "",
  174. "type": "scenario",
  175. "steps":
  176. [{"keyword": "Given ",
  177. "name": "there are bananas",
  178. "line": 4,
  179. "match": {"location": "spec/cucumber/formatter/json_spec.rb:180"},
  180. "result": {"status": "passed",
  181. "duration": 1}}]}]}]))
  182. end
  183. end
  184. describe 'with tags in the feature file' do
  185. define_feature <<-FEATURE
  186. @f
  187. Feature: Banana party
  188. @s
  189. Scenario: Monkey eats bananas
  190. Given there are bananas
  191. @so
  192. Scenario Outline: Monkey eats bananas
  193. Given there are <fruit>
  194. @ex
  195. Examples: Fruit Table
  196. | fruit |
  197. | bananas |
  198. FEATURE
  199. define_steps do
  200. Given(/^there are bananas$/) {}
  201. end
  202. it 'the tags are included in the json data' do
  203. expect(load_normalised_json(@out)).to eq JSON.parse(%(
  204. [{"id": "banana-party",
  205. "uri": "spec.feature",
  206. "keyword": "Feature",
  207. "name": "Banana party",
  208. "line": 2,
  209. "description": "",
  210. "tags": [{"name": "@f",
  211. "line": 1}],
  212. "elements":
  213. [{"id": "banana-party;monkey-eats-bananas",
  214. "keyword": "Scenario",
  215. "name": "Monkey eats bananas",
  216. "line": 5,
  217. "description": "",
  218. "tags": [{"name": "@f",
  219. "line": 1},
  220. {"name": "@s",
  221. "line": 4}],
  222. "type": "scenario",
  223. "steps":
  224. [{"keyword": "Given ",
  225. "name": "there are bananas",
  226. "line": 6,
  227. "match": {"location": "spec/cucumber/formatter/json_spec.rb:228"},
  228. "result": {"status": "passed",
  229. "duration": 1}}]},
  230. {"id": "banana-party;monkey-eats-bananas;fruit-table;2",
  231. "keyword": "Scenario Outline",
  232. "name": "Monkey eats bananas",
  233. "line": 15,
  234. "description": "",
  235. "tags": [{"name": "@f",
  236. "line": 1},
  237. {"name": "@so",
  238. "line": 8},
  239. {"name": "@ex",
  240. "line": 12}],
  241. "type": "scenario",
  242. "steps":
  243. [{"keyword": "Given ",
  244. "name": "there are bananas",
  245. "line": 10,
  246. "match": {"location": "spec/cucumber/formatter/json_spec.rb:228"},
  247. "result": {"status": "passed",
  248. "duration": 1}}]}]}]))
  249. end
  250. end
  251. describe 'with comments in the feature file' do
  252. define_feature <<-FEATURE
  253. #feature comment
  254. Feature: Banana party
  255. #background comment
  256. Background: There are bananas
  257. Given there are bananas
  258. #scenario comment
  259. Scenario: Monkey eats bananas
  260. #step comment1
  261. Then the monkey eats bananas
  262. #scenario outline comment
  263. Scenario Outline: Monkey eats bananas
  264. #step comment2
  265. Then the monkey eats <fruit>
  266. #examples table comment
  267. Examples: Fruit Table
  268. | fruit |
  269. #examples table row comment
  270. | bananas |
  271. FEATURE
  272. define_steps do
  273. Given(/^there are bananas$/) {}
  274. Then(/^the monkey eats bananas$/) {}
  275. end
  276. it 'the comments are not included in the json data' do
  277. expect(load_normalised_json(@out)).to eq JSON.parse(%(
  278. [{"id": "banana-party",
  279. "uri": "spec.feature",
  280. "keyword": "Feature",
  281. "name": "Banana party",
  282. "line": 2,
  283. "description": "",
  284. "elements":
  285. [{"keyword": "Background",
  286. "name": "There are bananas",
  287. "line": 5,
  288. "description": "",
  289. "type": "background",
  290. "steps":
  291. [{"keyword": "Given ",
  292. "name": "there are bananas",
  293. "line": 6,
  294. "match": {"location": "spec/cucumber/formatter/json_spec.rb:308"},
  295. "result": {"status": "passed",
  296. "duration": 1}}]},
  297. {"id": "banana-party;monkey-eats-bananas",
  298. "keyword": "Scenario",
  299. "name": "Monkey eats bananas",
  300. "line": 9,
  301. "description": "",
  302. "type": "scenario",
  303. "steps":
  304. [{"keyword": "Then ",
  305. "name": "the monkey eats bananas",
  306. "line": 11,
  307. "match": {"location": "spec/cucumber/formatter/json_spec.rb:309"},
  308. "result": {"status": "passed",
  309. "duration": 1}}]},
  310. {"keyword": "Background",
  311. "name": "There are bananas",
  312. "line": 5,
  313. "description": "",
  314. "type": "background",
  315. "steps":
  316. [{"keyword": "Given ",
  317. "name": "there are bananas",
  318. "line": 6,
  319. "match": {"location": "spec/cucumber/formatter/json_spec.rb:308"},
  320. "result": {"status": "passed",
  321. "duration": 1}}]},
  322. {"id": "banana-party;monkey-eats-bananas;fruit-table;2",
  323. "keyword": "Scenario Outline",
  324. "name": "Monkey eats bananas",
  325. "line": 22,
  326. "description": "",
  327. "type": "scenario",
  328. "steps":
  329. [{"keyword": "Then ",
  330. "name": "the monkey eats bananas",
  331. "line": 16,
  332. "match": {"location": "spec/cucumber/formatter/json_spec.rb:309"},
  333. "result": {"status": "passed",
  334. "duration": 1}}]}]}]))
  335. end
  336. end
  337. describe 'with a scenario with a step with a doc string' do
  338. define_feature <<-FEATURE
  339. Feature: Banana party
  340. Scenario: Monkey eats bananas
  341. Given there are bananas
  342. """
  343. the doc string
  344. """
  345. FEATURE
  346. define_steps do
  347. Given(/^there are bananas$/) { |s| s }
  348. end
  349. it 'includes the doc string in the json data' do
  350. expect(load_normalised_json(@out)).to eq JSON.parse(%(
  351. [{"id": "banana-party",
  352. "uri": "spec.feature",
  353. "keyword": "Feature",
  354. "name": "Banana party",
  355. "line": 1,
  356. "description": "",
  357. "elements":
  358. [{"id": "banana-party;monkey-eats-bananas",
  359. "keyword": "Scenario",
  360. "name": "Monkey eats bananas",
  361. "line": 3,
  362. "description": "",
  363. "type": "scenario",
  364. "steps":
  365. [{"keyword": "Given ",
  366. "name": "there are bananas",
  367. "line": 4,
  368. "doc_string": {"value": "the doc string",
  369. "content_type": "",
  370. "line": 5},
  371. "match": {"location": "spec/cucumber/formatter/json_spec.rb:386"},
  372. "result": {"status": "passed",
  373. "duration": 1}}]}]}]))
  374. end
  375. end
  376. describe 'with a scenario with a step that use puts' do
  377. define_feature <<-FEATURE
  378. Feature: Banana party
  379. Scenario: Monkey eats bananas
  380. Given there are bananas
  381. FEATURE
  382. define_steps do
  383. Given(/^there are bananas$/) { log 'from step' }
  384. end
  385. it 'includes the output from the step in the json data' do
  386. expect(load_normalised_json(@out)).to eq JSON.parse(%(
  387. [{"id": "banana-party",
  388. "uri": "spec.feature",
  389. "keyword": "Feature",
  390. "name": "Banana party",
  391. "line": 1,
  392. "description": "",
  393. "elements":
  394. [{"id": "banana-party;monkey-eats-bananas",
  395. "keyword": "Scenario",
  396. "name": "Monkey eats bananas",
  397. "line": 3,
  398. "description": "",
  399. "type": "scenario",
  400. "steps":
  401. [{"keyword": "Given ",
  402. "name": "there are bananas",
  403. "line": 4,
  404. "output": ["from step"],
  405. "match": {"location": "spec/cucumber/formatter/json_spec.rb:426"},
  406. "result": {"status": "passed",
  407. "duration": 1}}]}]}]))
  408. end
  409. end
  410. describe 'with a background' do
  411. define_feature <<-FEATURE
  412. Feature: Banana party
  413. Background: There are bananas
  414. Given there are bananas
  415. Scenario: Monkey eats bananas
  416. Then the monkey eats bananas
  417. Scenario: Monkey eats more bananas
  418. Then the monkey eats more bananas
  419. FEATURE
  420. it 'includes the background in the json data each time it is executed' do
  421. expect(load_normalised_json(@out)).to eq JSON.parse(%(
  422. [{"id": "banana-party",
  423. "uri": "spec.feature",
  424. "keyword": "Feature",
  425. "name": "Banana party",
  426. "line": 1,
  427. "description": "",
  428. "elements":
  429. [{"keyword": "Background",
  430. "name": "There are bananas",
  431. "line": 3,
  432. "description": "",
  433. "type": "background",
  434. "steps":
  435. [{"keyword": "Given ",
  436. "name": "there are bananas",
  437. "line": 4,
  438. "match": {"location": "spec.feature:4"},
  439. "result": {"status": "undefined"}}]},
  440. {"id": "banana-party;monkey-eats-bananas",
  441. "keyword": "Scenario",
  442. "name": "Monkey eats bananas",
  443. "line": 6,
  444. "description": "",
  445. "type": "scenario",
  446. "steps":
  447. [{"keyword": "Then ",
  448. "name": "the monkey eats bananas",
  449. "line": 7,
  450. "match": {"location": "spec.feature:7"},
  451. "result": {"status": "undefined"}}]},
  452. {"keyword": "Background",
  453. "name": "There are bananas",
  454. "line": 3,
  455. "description": "",
  456. "type": "background",
  457. "steps":
  458. [{"keyword": "Given ",
  459. "name": "there are bananas",
  460. "line": 4,
  461. "match": {"location": "spec.feature:4"},
  462. "result": {"status": "undefined"}}]},
  463. {"id": "banana-party;monkey-eats-more-bananas",
  464. "keyword": "Scenario",
  465. "name": "Monkey eats more bananas",
  466. "line": 9,
  467. "description": "",
  468. "type": "scenario",
  469. "steps":
  470. [{"keyword": "Then ",
  471. "name": "the monkey eats more bananas",
  472. "line": 10,
  473. "match": {"location": "spec.feature:10"},
  474. "result": {"status": "undefined"}}]}]}]))
  475. end
  476. end
  477. describe 'with a scenario with a step that embeds data directly' do
  478. define_feature <<-FEATURE
  479. Feature: Banana party
  480. Scenario: Monkey eats bananas
  481. Given there are bananas
  482. FEATURE
  483. define_steps do
  484. Given(/^there are bananas$/) { attach('YWJj', 'mime-type;base64') }
  485. end
  486. it 'includes the data from the step in the json data' do
  487. expect(load_normalised_json(@out)).to eq JSON.parse(%(
  488. [{"id": "banana-party",
  489. "uri": "spec.feature",
  490. "keyword": "Feature",
  491. "name": "Banana party",
  492. "line": 1,
  493. "description": "",
  494. "elements":
  495. [{"id": "banana-party;monkey-eats-bananas",
  496. "keyword": "Scenario",
  497. "name": "Monkey eats bananas",
  498. "line": 3,
  499. "description": "",
  500. "type": "scenario",
  501. "steps":
  502. [{"keyword": "Given ",
  503. "name": "there are bananas",
  504. "line": 4,
  505. "embeddings": [{"mime_type": "mime-type",
  506. "data": "YWJj"}],
  507. "match": {"location": "spec/cucumber/formatter/json_spec.rb:536"},
  508. "result": {"status": "passed",
  509. "duration": 1}}]}]}]))
  510. end
  511. end
  512. describe 'with a scenario with a step that embeds a file' do
  513. define_feature <<-FEATURE
  514. Feature: Banana party
  515. Scenario: Monkey eats bananas
  516. Given there are bananas
  517. FEATURE
  518. define_steps do
  519. Given(/^there are bananas$/) do
  520. RSpec::Mocks.allow_message(File, :file?) { true }
  521. RSpec::Mocks.allow_message(File, :read) { 'foo' }
  522. attach('out/snapshot.jpeg', 'image/png')
  523. end
  524. end
  525. it 'includes the file content in the json data' do
  526. expect(load_normalised_json(@out)).to eq JSON.parse(%(
  527. [{"id": "banana-party",
  528. "uri": "spec.feature",
  529. "keyword": "Feature",
  530. "name": "Banana party",
  531. "line": 1,
  532. "description": "",
  533. "elements":
  534. [{"id": "banana-party;monkey-eats-bananas",
  535. "keyword": "Scenario",
  536. "name": "Monkey eats bananas",
  537. "line": 3,
  538. "description": "",
  539. "type": "scenario",
  540. "steps":
  541. [{"keyword": "Given ",
  542. "name": "there are bananas",
  543. "line": 4,
  544. "embeddings": [{"mime_type": "image/png",
  545. "data": "Zm9v"}],
  546. "match": {"location": "spec/cucumber/formatter/json_spec.rb:575"},
  547. "result": {"status": "passed",
  548. "duration": 1}}]}]}]))
  549. end
  550. end
  551. describe 'with a scenario with hooks' do
  552. define_feature <<-FEATURE
  553. Feature: Banana party
  554. Scenario: Monkey eats bananas
  555. Given there are bananas
  556. FEATURE
  557. define_steps do
  558. Before() {}
  559. Before() {}
  560. After() {}
  561. After() {}
  562. AfterStep() {}
  563. AfterStep() {}
  564. Around() { |_scenario, block| block.call }
  565. Given(/^there are bananas$/) {}
  566. end
  567. it 'includes all hooks except the around hook in the json data' do
  568. expect(load_normalised_json(@out)).to eq JSON.parse(%(
  569. [{"id": "banana-party",
  570. "uri": "spec.feature",
  571. "keyword": "Feature",
  572. "name": "Banana party",
  573. "line": 1,
  574. "description": "",
  575. "elements":
  576. [{"id": "banana-party;monkey-eats-bananas",
  577. "keyword": "Scenario",
  578. "name": "Monkey eats bananas",
  579. "line": 3,
  580. "description": "",
  581. "type": "scenario",
  582. "before":
  583. [{"match": {"location": "spec/cucumber/formatter/json_spec.rb:618"},
  584. "result": {"status": "passed",
  585. "duration": 1}},
  586. {"match": {"location": "spec/cucumber/formatter/json_spec.rb:619"},
  587. "result": {"status": "passed",
  588. "duration": 1}}],
  589. "steps":
  590. [{"keyword": "Given ",
  591. "name": "there are bananas",
  592. "line": 4,
  593. "match": {"location": "spec/cucumber/formatter/json_spec.rb:625"},
  594. "result": {"status": "passed",
  595. "duration": 1},
  596. "after":
  597. [{"match": {"location": "spec/cucumber/formatter/json_spec.rb:622"},
  598. "result": {"status": "passed",
  599. "duration": 1}},
  600. {"match": {"location": "spec/cucumber/formatter/json_spec.rb:623"},
  601. "result": {"status": "passed",
  602. "duration": 1}}]}],
  603. "after":
  604. [{"match": {"location": "spec/cucumber/formatter/json_spec.rb:621"},
  605. "result": {"status": "passed",
  606. "duration": 1}},
  607. {"match": {"location": "spec/cucumber/formatter/json_spec.rb:620"},
  608. "result": {"status": "passed",
  609. "duration": 1}}]}]}]))
  610. end
  611. end
  612. describe 'with a scenario when only an around hook is failing' do
  613. define_feature <<-FEATURE
  614. Feature: Banana party
  615. Scenario: Monkey eats bananas
  616. Given there are bananas
  617. FEATURE
  618. define_steps do
  619. Around() do |_scenario, block|
  620. block.call
  621. raise 'error'
  622. end
  623. Given(/^there are bananas$/) {}
  624. end
  625. it 'includes the around hook result in the json data' do
  626. expect(load_normalised_json(@out)).to eq JSON.parse(%{
  627. [{"id": "banana-party",
  628. "uri": "spec.feature",
  629. "keyword": "Feature",
  630. "name": "Banana party",
  631. "line": 1,
  632. "description": "",
  633. "elements":
  634. [{"id": "banana-party;monkey-eats-bananas",
  635. "keyword": "Scenario",
  636. "name": "Monkey eats bananas",
  637. "line": 3,
  638. "description": "",
  639. "type": "scenario",
  640. "steps":
  641. [{"keyword": "Given ",
  642. "name": "there are bananas",
  643. "line": 4,
  644. "match": {"location": "spec/cucumber/formatter/json_spec.rb:687"},
  645. "result": {"status": "passed",
  646. "duration": 1}}],
  647. "around":
  648. [{"match": {"location": "unknown_hook_location:1"},
  649. "result": {"status": "failed",
  650. "error_message": "error (RuntimeError)\\n./spec/cucumber/formatter/json_spec.rb:685:in `Around'",
  651. "duration": 1}}]}]}]})
  652. end
  653. end
  654. describe 'with a scenario with a step with a data table' do
  655. define_feature <<-FEATURE
  656. Feature: Banana party
  657. Scenario: Monkey eats bananas
  658. Given there are bananas
  659. | aa | bb |
  660. | cc | dd |
  661. FEATURE
  662. define_steps do
  663. Given(/^there are bananas$/) { |s| s }
  664. end
  665. it 'includes the doc string in the json data' do
  666. expect(load_normalised_json(@out)).to eq JSON.parse(%(
  667. [{"id": "banana-party",
  668. "uri": "spec.feature",
  669. "keyword": "Feature",
  670. "name": "Banana party",
  671. "line": 1,
  672. "description": "",
  673. "elements":
  674. [{"id": "banana-party;monkey-eats-bananas",
  675. "keyword": "Scenario",
  676. "name": "Monkey eats bananas",
  677. "line": 3,
  678. "description": "",
  679. "type": "scenario",
  680. "steps":
  681. [{"keyword": "Given ",
  682. "name": "there are bananas",
  683. "line": 4,
  684. "rows":
  685. [{"cells": ["aa", "bb"]},
  686. {"cells": ["cc", "dd"]}],
  687. "match": {"location": "spec/cucumber/formatter/json_spec.rb:731"},
  688. "result": {"status": "passed",
  689. "duration": 1}}]}]}]))
  690. end
  691. end
  692. end
  693. def load_normalised_json(out)
  694. normalise_json(JSON.parse(out.string))
  695. end
  696. def normalise_json(json)
  697. # make sure duration was captured (should be >= 0)
  698. # then set it to what is "expected" since duration is dynamic
  699. json.each do |feature|
  700. elements = feature.fetch('elements') { [] }
  701. elements.each do |scenario|
  702. %w[steps before after around].each do |type|
  703. next unless scenario[type]
  704. scenario[type].each do |step_or_hook|
  705. normalise_json_step_or_hook(step_or_hook)
  706. next unless step_or_hook['after']
  707. step_or_hook['after'].each do |hook|
  708. normalise_json_step_or_hook(hook)
  709. end
  710. end
  711. end
  712. end
  713. end
  714. end
  715. def normalise_json_step_or_hook(step_or_hook)
  716. return unless step_or_hook['result'] && step_or_hook['result']['duration']
  717. expect(step_or_hook['result']['duration']).to be >= 0
  718. step_or_hook['result']['duration'] = 1
  719. end
  720. end
  721. end
  722. end

No Description

Contributors (1)