<commit_before>require 'spec_helper'

describe Protobuf::Enum do
  let(:name) { :THREE }
  let(:tag)  { 3 }

  before(:all) do
    Test::EnumTestType.define(:MINUS_ONE, -1)
    Test::EnumTestType.define(name, tag)
  end

  describe '.define' do
    it 'defines a constant enum value on the parent class' do
      Test::EnumTestType.constants.should include(name)
      Test::EnumTestType::THREE.should be_a(Protobuf::EnumValue)
    end
  end

  describe '.fetch' do
    context 'when value is an EnumValue' do
      it 'responds with that object' do
        Test::EnumTestType.fetch(Test::EnumTestType::THREE).name.should eq Test::EnumTestType::THREE.name
      end
    end

    context 'when value can be coerced to a symbol' do
      it 'fetches based on the symbol name value' do
        Test::EnumTestType.fetch("ONE").should eq Test::EnumTestType::ONE
        Test::EnumTestType.fetch(:ONE).should eq Test::EnumTestType::ONE
      end
    end

    context 'when value can be coerced to an integer' do
      it 'fetches based on the integer tag value' do
        Test::EnumTestType.fetch(3.0).should eq Test::EnumTestType::THREE
        Test::EnumTestType.fetch(3).should eq Test::EnumTestType::THREE
      end
    end

    context 'when value is not an applicable type' do
      it 'returns a nil' do
        Test::EnumTestType.fetch(Test::Resource.new).should be_nil
        Test::EnumTestType.fetch(nil).should be_nil
        Test::EnumTestType.fetch(false).should be_nil
      end
    end
  end

  describe '.enum_by_value' do
    it 'gets the EnumValue corresponding to the given value (tag)' do
      Test::EnumTestType.enum_by_value(tag).should eq Test::EnumTestType.const_get(name)
    end
  end

  describe '.name_by_value' do
    it 'gets the name of the enum corresponding to the given value (tag)' do
      Test::EnumTestType.name_by_value(tag).should eq name
    end

    context 'when given name is nil' do
      it 'returns a nil' do
        Test::EnumTestType.name_by_value(nil).should be_nil
      end
    end
  end

  describe '.valid_tag?' do
    context 'when tag is defined' do
      specify { Test::EnumTestType.valid_tag?(tag).should be_true }<commit_msg>add spec for referencing name_by_value with enum<commit_after>require 'spec_helper'

describe Protobuf::Enum do
  let(:name) { :THREE }
  let(:tag)  { 3 }

  before(:all) do
    Test::EnumTestType.define(:MINUS_ONE, -1)
    Test::EnumTestType.define(name, tag)
  end

  describe '.define' do
    it 'defines a constant enum value on the parent class' do
      Test::EnumTestType.constants.should include(name)
      Test::EnumTestType::THREE.should be_a(Protobuf::EnumValue)
    end
  end

  describe '.fetch' do
    context 'when value is an EnumValue' do
      it 'responds with that object' do
        Test::EnumTestType.fetch(Test::EnumTestType::THREE).name.should eq Test::EnumTestType::THREE.name
      end
    end

    context 'when value can be coerced to a symbol' do
      it 'fetches based on the symbol name value' do
        Test::EnumTestType.fetch("ONE").should eq Test::EnumTestType::ONE
        Test::EnumTestType.fetch(:ONE).should eq Test::EnumTestType::ONE
      end
    end

    context 'when value can be coerced to an integer' do
      it 'fetches based on the integer tag value' do
        Test::EnumTestType.fetch(3.0).should eq Test::EnumTestType::THREE
        Test::EnumTestType.fetch(3).should eq Test::EnumTestType::THREE
      end
    end

    context 'when value is not an applicable type' do
      it 'returns a nil' do
        Test::EnumTestType.fetch(Test::Resource.new).should be_nil
        Test::EnumTestType.fetch(nil).should be_nil
        Test::EnumTestType.fetch(false).should be_nil
      end
    end
  end

  describe '.enum_by_value' do
    it 'gets the EnumValue corresponding to the given value (tag)' do
      Test::EnumTestType.enum_by_value(tag).should eq Test::EnumTestType.const_get(name)
    end
  end

  describe '.name_by_value' do
    it 'get the name by value of the enum given the enum' do 
      Test::EnumTestType.name_by_value(::Test::EnumTestType::THREE).should eq name
    end

    it 'gets the name of the enum corresponding to the given value (tag)' do
      Test::EnumTestType.name_by_value(tag).should eq name
    end

    context 'when given name is nil' do
      it 'returns a nil' do
        Test::EnumTestType.name_by_value(nil).should be_nil
      end
    end
  end

  describe '.valid_tag?' do
    context 'when tag is defined' do
      specify { Test::EnumTestType.valid_tag?(tag).should be_true }
{'fraction_non_alphanumeric': 0.09576103667911268, 'fraction_numerical': 0.0021963540522732263, 'mean_word_length': 3.1779816513761467, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2338235', 'n_tokens_mistral': 1427, 'n_tokens_neox': 1399, 'n_words': 429}