|
| 1 | +# Run with: |
| 2 | +# rspec --format doc --order defined examples/test_construct_spec.rb |
| 3 | + |
| 4 | +# We can't replace the usage of the instance variable with a let or method call, |
| 5 | +# since the example isn't available then, so we disable the warning: |
| 6 | +# rubocop:disable RSpec/InstanceVariable |
| 7 | + |
| 8 | +require "test_construct/rspec_integration" |
| 9 | + |
| 10 | +RSpec.describe TestConstruct do |
| 11 | + before do |
| 12 | + stub_const("TEST_PATH", "alice/rabbithole") |
| 13 | + stub_const("TEST_FILE", "white_rabbit.txt") |
| 14 | + stub_const("TEST_CONTENTS", "I'm late!") |
| 15 | + end |
| 16 | + |
| 17 | + describe "when not enabled in describe block" do |
| 18 | + it "is disabled in examples" do |example| |
| 19 | + expect(example.metadata).not_to include(:construct) |
| 20 | + end |
| 21 | + |
| 22 | + it "can be enabled for example", test_construct: true do |example| |
| 23 | + expect(example.metadata).to include(:construct) |
| 24 | + end |
| 25 | + |
| 26 | + context "when enabled in context", test_construct: true do |
| 27 | + it "is enabled in examples" do |example| |
| 28 | + expect(example.metadata).to include(:construct) |
| 29 | + end |
| 30 | + end |
| 31 | + end |
| 32 | + |
| 33 | + describe "when enabled in describe block", test_construct: true do |
| 34 | + before { |example| @construct = example.metadata[:construct] } |
| 35 | + |
| 36 | + it "can be disabled for an example", test_construct: false do |
| 37 | + expect(@context).to be_nil |
| 38 | + end |
| 39 | + |
| 40 | + context "when disabled in context", test_construct: false do |
| 41 | + it "is disabled in examples" do |example| |
| 42 | + expect(example.metadata).not_to include(:construct) |
| 43 | + end |
| 44 | + end |
| 45 | + |
| 46 | + it "leaves file on error" do |
| 47 | + @construct.directory TEST_PATH do |path| |
| 48 | + path.file(TEST_FILE, TEST_CONTENTS) |
| 49 | + raise "Expected to fail" |
| 50 | + end |
| 51 | + end |
| 52 | + |
| 53 | + context "when keep_on_error is disabled for an example" do |
| 54 | + it "doesn't leave file", test_construct: { keep_on_error: false } do |
| 55 | + @construct.directory TEST_PATH do |path| |
| 56 | + path.file TEST_FILE, TEST_CONTENTS |
| 57 | + raise "Expected to fail" |
| 58 | + end |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + describe ".directory" do |
| 63 | + it "creates directory" do |
| 64 | + @construct.directory("alice") |
| 65 | + expect(Dir.exist?("alice")).to be true |
| 66 | + end |
| 67 | + |
| 68 | + it "creates directory and sudirectories" do |
| 69 | + @construct.directory(TEST_PATH) |
| 70 | + expect(Dir.exist?(TEST_PATH)).to be true |
| 71 | + end |
| 72 | + |
| 73 | + it "runs block in directory" do |
| 74 | + @construct.directory TEST_PATH do |path| |
| 75 | + path.file TEST_FILE, TEST_CONTENTS |
| 76 | + end |
| 77 | + filename = File.join(TEST_PATH, TEST_FILE) |
| 78 | + expect(File.read(filename)).to eq(TEST_CONTENTS) |
| 79 | + end |
| 80 | + end |
| 81 | + |
| 82 | + describe ".file" do |
| 83 | + it "creates empty file" do |
| 84 | + @construct.file(TEST_FILE) |
| 85 | + expect(File.read(TEST_FILE)).to eq("") |
| 86 | + end |
| 87 | + |
| 88 | + it "creates file with content from optional argument" do |
| 89 | + @construct.file(TEST_FILE, TEST_CONTENTS) |
| 90 | + expect(File.read(TEST_FILE)).to eq(TEST_CONTENTS) |
| 91 | + end |
| 92 | + |
| 93 | + it "creates file with content from block" do |
| 94 | + @construct.file(TEST_FILE) { TEST_CONTENTS } |
| 95 | + expect(File.read(TEST_FILE)).to eq(TEST_CONTENTS) |
| 96 | + end |
| 97 | + |
| 98 | + it "creates file and provides block IO object" do |
| 99 | + @construct.file(TEST_FILE) { |file| file << TEST_CONTENTS } |
| 100 | + expect(File.read(TEST_FILE)).to eq(TEST_CONTENTS) |
| 101 | + end |
| 102 | + end |
| 103 | + end |
| 104 | + |
| 105 | + describe "when keep_on_error is disabled in describe block", |
| 106 | + test_construct: { keep_on_error: false } do |
| 107 | + it "doesn't leave file on error" do |example| |
| 108 | + example.metadata[:construct].directory TEST_PATH do |path| |
| 109 | + path.file TEST_FILE, TEST_CONTENTS |
| 110 | + raise "Expected to fail" |
| 111 | + end |
| 112 | + end |
| 113 | + end |
| 114 | +end |
| 115 | + |
| 116 | +# rubocop:enable RSpec/InstanceVariable |
0 commit comments