File: converter_registry_spec.rb

package info (click to toggle)
ruby-tty-prompt 0.23.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,452 kB
  • sloc: ruby: 8,847; makefile: 4
file content (74 lines) | stat: -rw-r--r-- 2,066 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# frozen_string_literal: true

RSpec.describe TTY::Prompt::ConverterRegistry do
  context "contain" do
    it "doesn't have conversion" do
      registry = described_class.new
      expect(registry.contain?(:unknown)).to eq(false)
    end

    it "contains conversion" do
      registry = described_class.new(foo: ->(val) { val })
      expect(registry.contain?(:foo)).to eq(true)
    end

    it "checks conversion with object type" do
      registry = described_class.new(integer: ->(val) { val })
      expect(registry.contain?(Integer)).to eq(true)
    end
  end

  context "register" do
    it "registers new conversion under single name" do
      registry = described_class.new

      registry.register(:foo) { |val| val }

      expect(registry.contain?(:foo)).to eq(true)
    end

    it "registers new conversion under multiple names" do
      registry = described_class.new

      registry.register(:foo, :fum) { |val| val }

      expect(registry.contain?(:foo)).to eq(true)
      expect(registry.contain?(:fum)).to eq(true)
    end

    it "fails to register conversion" do
      registry = described_class.new(foo: ->(val) { val })

      expect {
        registry.register(:foo) { "value2" }
      }.to raise_error(TTY::Prompt::ConversionAlreadyDefined,
                       "converter for :foo is already registered")
    end
  end

  context "fetch" do
    it "retrieves converter from the registry" do
      conversion = ->(val) { val }
      registry = described_class.new(foo: conversion)

      expect(registry[:foo]).to eq(conversion)
      expect(registry.fetch(:foo)).to eq(conversion)
    end

    it "retrieves uppcase named converter" do
      conversion = ->(val) { val }
      registry = described_class.new(foo: conversion)

      expect(registry["FOO"]).to eq(conversion)
    end

    it "fails to retrieve conversion" do
      registry = described_class.new

      expect {
        registry[:foo]
      }.to raise_error(TTY::Prompt::UnsupportedConversion,
                       "converter :foo is not registered")
    end
  end
end