Skip to content

Instantly share code, notes, and snippets.

@Dysp

Dysp/_kincom.rb Secret

Last active August 12, 2016 18:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dysp/17e8f1bff3b5823d232a04a8711a4967 to your computer and use it in GitHub Desktop.
Save Dysp/17e8f1bff3b5823d232a04a8711a4967 to your computer and use it in GitHub Desktop.
class KincomValidator
def initialize(file, options)
@file = file
@options = options
end
def validate
is_file_valid?
is_options_valid?
end
#Private
def is_file_valid?
validate_file_extension
end
def is_options_valid?
validate_options_hash
validate_begin_percentage(@options[:begin_percentage])
validate_prestress(@options[:prestress])
validate_stop_index(@options[:stop_index])
validate_arm(@options[:arm])
validate_name(@options[:name])
validate_time_values(@options[:time_values])
end
def validate_file_extension
if @file.class.extname(@file) != '.xlsx'
raise ArgumentError, 'File-type is wrong. Can only read .xlsx files.'
end
end
def validate_options_hash
@options.each do |key, value|
if key.nil? || value.nil?
raise ArgumentError, "The options_hash contains a nil. Shouldn't, really."
end
end
end
def validate_begin_percentage(begin_percentage)
if !begin_percentage.is_a? Float
raise ArgumentError, "Begin percentage: '#{begin_percentage}' must be a Float. It is a #{begin_percentage.class}."
end
if begin_percentage <= 0 || begin_percentage >= 1
raise ArgumentError, 'Begin percentage must be between 0 and 1'
end
end
def validate_prestress(prestress)
if !prestress.is_a? Float
raise ArgumentError, "Prestress: '#{prestress}' must be a Float. It is a #{prestress.class}."
end
end
def validate_stop_index(stop_index)
if !stop_index.is_a? Float
raise ArgumentError, "Stop_index: '#{stop_index}' must be a Float. It is a #{stop_index.class}."
end
if stop_index <= 0 || stop_index >= 1
raise ArgumentError, 'Stop_index must be between 0 and 1'
end
end
def validate_arm(arm)
if !arm.is_a? Float
raise ArgumentError, "Arm: '#{arm}' must be a Float. It is a #{arm.class}."
end
end
def validate_name(name)
if !name.is_a? String
raise ArgumentError, "Name: '#{name}' must be a String. It is a #{name.class}."
end
end
def validate_time_values(time_values)
if time_values != time_values.sort
raise ArgumentError, 'Time values should be of ascending sizes'
end
end
end
private :is_file_valid?, :is_options_valid?, :validate_file_extension, :validate_options_hash,
:validate_begin_percentage, :validate_prestress, :validate_stop_index, :validate_arm,
:validate_time_values
ERROR["test_validate_options", TestValidation, 0.0015510980156250298]
test_validate_options#TestValidation (0.00s)
NameError: NameError: undefined method `is_file_valid?' for class `Object'
D:/Arbejde/ResearchLab/Plugin/xercise/validation/_kincom.rb:85:in `private'
D:/Arbejde/ResearchLab/Plugin/xercise/validation/_kincom.rb:85:in `<top (required)>'
D:/Arbejde/ResearchLab/Plugin/xercise/validation/validation_helper.rb:3:in `require_relative'
D:/Arbejde/ResearchLab/Plugin/xercise/validation/validation_helper.rb:3:in `kincom'
D:/Arbejde/ResearchLab/Plugin/myowntest/test_xerciseclass/test_validation/validation_test.rb:7:in `setup'
ERROR["test_validate_file", TestValidation, 0.004487149009946734]
test_validate_file#TestValidation (0.00s)
NameError: NameError: undefined method `is_file_valid?' for class `Object'
D:/Arbejde/ResearchLab/Plugin/xercise/validation/_kincom.rb:85:in `private'
D:/Arbejde/ResearchLab/Plugin/xercise/validation/_kincom.rb:85:in `<top (required)>'
D:/Arbejde/ResearchLab/Plugin/xercise/validation/validation_helper.rb:3:in `require_relative'
D:/Arbejde/ResearchLab/Plugin/xercise/validation/validation_helper.rb:3:in `kincom'
D:/Arbejde/ResearchLab/Plugin/myowntest/test_xerciseclass/test_validation/validation_test.rb:7:in `setup'
2/2: [===================================] 100% Time: 00:00:00, Time: 00:00:00
Finished in 0.00828s
2 tests, 0 assertions, 0 failures, 2 errors, 0 skips
module ValidationHelper
def self.kincom
require_relative '_kincom'
end
end
require_relative '../../../xercise/validation/validation_helper.rb'
class TestValidation < Minitest::Test
include ValidationHelper
def setup
ValidationHelper::kincom
@valid = KincomValidator.new(FILE, OPTIONS)
@invalid_file = KincomValidator.new(INVALID_FILE, OPTIONS)
@invalid_options = KincomValidator.new(FILE, INVALID_OPTIONS)
@invalid_both = KincomValidator.new(INVALID_FILE, INVALID_OPTIONS)
@nil_options = KincomValidator.new(FILE, NIL_OPTIONS)
end
def test_validate_file
# Kan Validation håndtere en forkert filtype?
assert_raises ArgumentError do
@invalid_file.validate
@invalid_both.validate
end
end
def test_validate_options
# Kan Validation håndtere forkerte optionsværdier?
INVALID_OPTIONS.each do |key, value|
assert_raises ArgumentError do
@invalid_options.validate
@invalid_both.validate
end
end
# Kan Validation håndtere nil i options?
NIL_OPTIONS.each do |key, value|
assert_raises ArgumentError do
@nil_options.validate
end
end
end
end
require_relative './validation/validation_helper.rb'
module Xercise
include ValidationHelper
def self.kincom(file, options)
ValidationHelper.kincom
KincomValidator.new(file, options).validate
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment