Class: Judges::Categories

Inherits:
Object show all
Defined in:
lib/judges/categories.rb

Overview

Categories of tests.

This class manages test categories, allowing you to enable or disable specific categories of tests. It provides a mechanism to filter which tests should be executed based on their associated categories.

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2024-2025 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Constructor Details

#initialize(enable, disable) ⇒ Categories

Initialize a new Categories instance.

Creates a categories filter with lists of enabled and disabled categories. The filter logic works as follows:

  • If a category is in the disable list, the test is rejected

  • If a category is in the enable list, the test is accepted

  • If no categories are enabled (empty enable list), all tests are accepted unless explicitly disabled

Parameters:

  • enable (Array<String>)

    List of categories to enable

  • disable (Array<String>)

    List of categories to disable



29
30
31
32
# File 'lib/judges/categories.rb', line 29

def initialize(enable, disable)
  @enable = enable.is_a?(Array) ? enable : []
  @disable = disable.is_a?(Array) ? disable : []
end

Instance Method Details

#ok?(cats) ⇒ Boolean

Check if a test with given categories should be executed.

Determines whether a test associated with the provided categories should be executed based on the enable/disable lists configured during initialization.

The evaluation logic:

  1. If any category is in the disable list, returns false

  2. If any category is in the enable list, returns true

  3. If the enable list is empty, returns true (all tests allowed)

  4. Otherwise, returns false

Examples:

Check if a test with categories should run

categories = Judges::Categories.new(['important'], ['experimental'])
categories.ok?(['important', 'slow']) # => true
categories.ok?(['experimental']) # => false
categories.ok?(nil) # => true (if enable list is empty)

Parameters:

  • cats (Array<String>, String, nil)

    List of categories associated with the test, can be a single string or nil

Returns:

  • (Boolean)

    true if the test should be executed, false otherwise



54
55
56
57
58
59
60
61
62
63
# File 'lib/judges/categories.rb', line 54

def ok?(cats)
  cats = [] if cats.nil?
  cats = [cats] unless cats.is_a?(Array)
  cats.each do |c|
    return false if @disable.any?(c)
    return true if @enable.any?(c)
  end
  return true if @enable.empty?
  false
end