Sha256: c8096bb946aaf0d83179616dbe2a5eb09f94825ad29e9f53667e6333f413c519

Contents?: true

Size: 1.79 KB

Versions: 5

Compression:

Stored size: 1.79 KB

Contents

module Mongoid::Acts::NestedSet
  module Matchers

    def have_nestedset_pos(lft, rgt, options = {})
      NestedSetPosition.new(lft, rgt, options)
    end

    class NestedSetPosition

      def initialize(lft, rgt, options)
        @lft = lft
        @rgt = rgt
        @options = options
      end

      def matches?(node)
        @node = node
        !!(
          node.respond_to?('left') && node.respond_to?('right') &&
          node.left == @lft &&
          node.right == @rgt
        )
      end

      def description
        "have position {left: #{@lft}, right: #{@rgt}}"
      end

      def failure_message_for_should
        sprintf("expected nested set position: {left: %2s, right: %2s}\n" +
                "                         got: {left: %2s, right: %2s}",
          @lft,
          @rgt,
          @node.respond_to?('left')  ? @node.left  : '?',
          @node.respond_to?('right') ? @node.right : '?'
        )
      end

      def failure_message
        sprintf("expected nested set position: {left: %2s, right: %2s}\n" +
                "                         got: {left: %2s, right: %2s}",
          @lft,
          @rgt,
          @node.respond_to?('left')  ? @node.left  : '?',
          @node.respond_to?('right') ? @node.right : '?'
        )
      end

      def failure_message_when_negated
        sprintf("expected nested set to not have position: {left: %2s, right: %2s}\n" +
                "                         got: {left: %2s, right: %2s}",
          @lft,
          @rgt,
          @node.respond_to?('left')  ? @node.left  : '?',
          @node.respond_to?('right') ? @node.right : '?'
        )
      end

      def failure_message_for_should_not
        sprintf("expected nested set to not have position: {left: %2s, right: %2s}", @lft, @rgt)
      end

    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
glebtv-mongoid_nested_set-0.8.0 spec/matchers/nestedset_pos.rb
glebtv-mongoid_nested_set-0.7.0 spec/matchers/nestedset_pos.rb
glebtv-mongoid_nested_set-0.6.0 spec/matchers/nestedset_pos.rb
glebtv-mongoid_nested_set-0.5.3 spec/matchers/nestedset_pos.rb
glebtv-mongoid_nested_set-0.5.2 spec/matchers/nestedset_pos.rb