started with a complete new state machine
This commit is contained in:
parent
93ef6f8970
commit
0daeeb713c
@ -1,29 +1,5 @@
|
||||
require 'working_class/version'
|
||||
require 'working_class/parser'
|
||||
require 'working_class/state_machine'
|
||||
require 'working_class/serializer'
|
||||
require 'working_class/task'
|
||||
require 'working_class/tasklist'
|
||||
|
||||
# WorkingClass Module
|
||||
#
|
||||
module WorkingClass
|
||||
|
||||
# Loads the file from the path and returns a Tasklist
|
||||
#
|
||||
# @param path [String] the filepath
|
||||
# @return [WorkingClass::Tasklist] the parsed Tasklist
|
||||
#
|
||||
def self.load_file(path)
|
||||
string = File.open(path, 'r').read()
|
||||
self.load(string)
|
||||
end
|
||||
|
||||
# Parses the given string and returns a Tasklist
|
||||
#
|
||||
# @param string [String] the WorkingClass tasklist syntax string
|
||||
# @return [WorkingClass::Tasklist] the parsed Tasklist
|
||||
#
|
||||
def self.load(string)
|
||||
Parser.new(string).to_tasklist
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -1,165 +0,0 @@
|
||||
require 'date'
|
||||
|
||||
module WorkingClass
|
||||
# The actual syntax parser
|
||||
#
|
||||
class Parser
|
||||
|
||||
# Initializes a new Parser object with a given WorkingClass syntax string
|
||||
#
|
||||
# @param string [String] the raw string with the WorkingClass syntax
|
||||
# @return [WorkingClass::Parser] a parser instance
|
||||
#
|
||||
def initialize(string)
|
||||
@raw_string = string
|
||||
@date_regex = /(?:\d{1,2}\.){2}\d{2,4}/
|
||||
@time_regex = /\d{1,2}:\d{1,2}/
|
||||
end
|
||||
|
||||
# Parses the syntax and returns an Hash with the result
|
||||
#
|
||||
# @return [Hash] a Hash representation of the parsed tasklist
|
||||
#
|
||||
def to_h
|
||||
{
|
||||
name: tasklist_name,
|
||||
tasks_count: tasks.length,
|
||||
tasks: tasks
|
||||
}
|
||||
end
|
||||
|
||||
# Parses the syntax and returns a WorkingClass::Tasklist instance
|
||||
#
|
||||
# @return [WorkingClass::Tasklist] a Tasklist instance of the parsed tasklist
|
||||
#
|
||||
def to_tasklist
|
||||
tasks = Array.new
|
||||
raw = self.to_h
|
||||
raw[:tasks].each do |t|
|
||||
task = Task.new(t[:name], t)
|
||||
tasks << task
|
||||
end
|
||||
Tasklist.new raw[:name], tasks
|
||||
end
|
||||
|
||||
private
|
||||
def tasklist_name_regex
|
||||
/(.*)\n---/
|
||||
end
|
||||
|
||||
def task_regex
|
||||
/\[(.*)\] *(?:\{(.*)\})? *(?:\((.*)\)){0,1} *(.*)/
|
||||
end
|
||||
|
||||
def tasklist_name
|
||||
@raw_string.match(tasklist_name_regex).captures.first
|
||||
end
|
||||
|
||||
def tasks
|
||||
tasks = []
|
||||
@raw_string.scan(task_regex).each do |match|
|
||||
task = Hash.new
|
||||
task[:is_finished] = extract_is_finished(match[0])
|
||||
task[:date] = extract_date(match[1])
|
||||
task[:reminder] = extract_reminder(task[:date], match[2])
|
||||
task[:name] = extract_name(match[3])
|
||||
tasks << task
|
||||
end
|
||||
tasks
|
||||
end
|
||||
|
||||
def extract_is_finished(match)
|
||||
match = match.strip
|
||||
if match == "x" or match == "X"
|
||||
true
|
||||
else
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
def extract_date(match)
|
||||
if match.nil?
|
||||
return nil
|
||||
end
|
||||
|
||||
if match.scan(@date_regex)
|
||||
# @todo rescue the right exception
|
||||
begin
|
||||
normalize_date(match)
|
||||
rescue
|
||||
nil
|
||||
end
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def extract_name(match)
|
||||
if !match.nil?
|
||||
match.strip
|
||||
else
|
||||
""
|
||||
end
|
||||
end
|
||||
|
||||
def normalize_date(date_string)
|
||||
parts = date_string.split(".")
|
||||
if parts[2].length == 2
|
||||
parts[2] = "20" + parts[2]
|
||||
end
|
||||
parts = parts.map { |p| p.to_i }
|
||||
Date.new(parts[2], parts[1], parts[0])
|
||||
end
|
||||
|
||||
# Shame Shame
|
||||
# @todo REFACTOR!!!!!1eleven!1
|
||||
def extract_reminder(task_date, match)
|
||||
if match.nil?
|
||||
return nil
|
||||
end
|
||||
|
||||
if match.empty? and !task_date.nil?
|
||||
return DateTime.parse(task_date.to_s + " 9:00")
|
||||
elsif match.empty? and task_date.nil?
|
||||
return nil
|
||||
end
|
||||
|
||||
date = nil
|
||||
time = nil
|
||||
|
||||
absolute_date = match.scan(/(#{@date_regex})/)
|
||||
relative_date = match.scan(/(-\d{1,2})/)
|
||||
match_time = match.scan(/(#{@time_regex})/)
|
||||
|
||||
if absolute_date.empty? and (!relative_date.empty? and !task_date.nil?)
|
||||
date = task_date + relative_date.flatten.first.to_i
|
||||
elsif !absolute_date.empty?
|
||||
# @todo rescue the right exception
|
||||
begin
|
||||
date = normalize_date(absolute_date.flatten.first)
|
||||
rescue
|
||||
date = nil
|
||||
end
|
||||
end
|
||||
|
||||
if !match_time.empty?
|
||||
time = match_time
|
||||
end
|
||||
|
||||
if !date.nil? and !time.nil?
|
||||
DateTime.parse(date.to_s + " #{time}")
|
||||
elsif (date.nil? and !task_date.nil?) and !time.nil?
|
||||
DateTime.parse(task_date.to_s + " #{time}")
|
||||
elsif !date.nil? and time.nil?
|
||||
DateTime.parse(date.to_s + " 9:00")
|
||||
else
|
||||
return nil
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
end
|
||||
|
3
lib/working_class/serializer.rb
Normal file
3
lib/working_class/serializer.rb
Normal file
@ -0,0 +1,3 @@
|
||||
require 'working_class/serializer/state_machine_serializer'
|
||||
require 'working_class/serializer/task_serializer'
|
||||
require 'working_class/serializer/tasklist_serializer'
|
36
lib/working_class/serializer/state_machine_serializer.rb
Normal file
36
lib/working_class/serializer/state_machine_serializer.rb
Normal file
@ -0,0 +1,36 @@
|
||||
module WorkingClass
|
||||
module Serializer
|
||||
|
||||
class StateMachineTransitionSerializer
|
||||
def serialize(transition)
|
||||
"\"#{transition.from.to_s}\" -> \"#{transition.to.to_s}\"" +
|
||||
" [ label = \"#{transition.regex.to_s}\" ];\n"
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
class StateMachineSerializer
|
||||
def initialize(state_machine_transition_serializer)
|
||||
@transition_serializer = state_machine_transition_serializer
|
||||
end
|
||||
|
||||
def serialize(state_machine)
|
||||
end_states = state_machine.end_states.map do |s|
|
||||
s.to_s
|
||||
end.join(" ")
|
||||
|
||||
transitions = state_machine.transitions.map do |t|
|
||||
@transition_serializer.serialize(t)
|
||||
end.join(" ")
|
||||
|
||||
output = """digraph finite_state_machine {
|
||||
rankdir=LR;
|
||||
size=\"8,5\";
|
||||
node[shape = doublecircle]; #{end_states};
|
||||
node[shape = circle];
|
||||
#{transitions}}"""
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
14
lib/working_class/serializer/task_serializer.rb
Normal file
14
lib/working_class/serializer/task_serializer.rb
Normal file
@ -0,0 +1,14 @@
|
||||
module WorkingClass
|
||||
module Serializer
|
||||
|
||||
class TaskSerializer
|
||||
def serialize(t)
|
||||
is_finished = t.is_finished ? "X" : " "
|
||||
name = t.name
|
||||
reminder = t.reminder.nil? ? "" : t.reminder.strftime("%m.%d.%Y %I:%M%P")
|
||||
"[#{is_finished}]{#{reminder}} #{name}\n"
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
15
lib/working_class/serializer/tasklist_serializer.rb
Normal file
15
lib/working_class/serializer/tasklist_serializer.rb
Normal file
@ -0,0 +1,15 @@
|
||||
module WorkingClass
|
||||
module Serializer
|
||||
class TasklistSerializer
|
||||
def initialize(task_serializer)
|
||||
@task_serializer = task_serializer
|
||||
end
|
||||
|
||||
def serialize(tasklist)
|
||||
output = "#{tasklist.name}\n---\n"
|
||||
tasklist.tasks.each { |t| output += @task_serializer.serialize(t) }
|
||||
output
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
4
lib/working_class/state_machine.rb
Normal file
4
lib/working_class/state_machine.rb
Normal file
@ -0,0 +1,4 @@
|
||||
require 'working_class/state_machine/transition'
|
||||
require 'working_class/state_machine/state'
|
||||
require 'working_class/state_machine/state_generator'
|
||||
require 'working_class/state_machine/state_machine'
|
21
lib/working_class/state_machine/state.rb
Normal file
21
lib/working_class/state_machine/state.rb
Normal file
@ -0,0 +1,21 @@
|
||||
module WorkingClass
|
||||
module StateMachine
|
||||
class State
|
||||
|
||||
attr_reader :name, :is_start, :is_end
|
||||
|
||||
def initialize(name, is_start, is_end)
|
||||
@name = name
|
||||
@is_start = is_start
|
||||
@is_end = is_end
|
||||
end
|
||||
|
||||
alias :is_start? :is_start
|
||||
alias :is_end? :is_end
|
||||
|
||||
def to_s
|
||||
self.name
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
14
lib/working_class/state_machine/state_generator.rb
Normal file
14
lib/working_class/state_machine/state_generator.rb
Normal file
@ -0,0 +1,14 @@
|
||||
module WorkingClass
|
||||
module StateMachine
|
||||
class StateGenerator
|
||||
def initialize
|
||||
@current_index = 0
|
||||
end
|
||||
|
||||
def generate_state(is_start, is_end)
|
||||
@current_index += 1
|
||||
State.new(@current_index, is_start, is_end)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
31
lib/working_class/state_machine/state_machine.rb
Normal file
31
lib/working_class/state_machine/state_machine.rb
Normal file
@ -0,0 +1,31 @@
|
||||
module WorkingClass
|
||||
module StateMachine
|
||||
class StateMachine
|
||||
|
||||
attr_reader :states, :transitions
|
||||
|
||||
def initialize(states, transitions)
|
||||
@states = states
|
||||
@transitions = transitions
|
||||
@processors = Hash.new
|
||||
end
|
||||
|
||||
def register_handler(transition, handler)
|
||||
if(@processors[transition] == nil)
|
||||
@processors[transition] = Array.new
|
||||
else
|
||||
@processors[transition] << handler
|
||||
end
|
||||
end
|
||||
|
||||
def start_state
|
||||
@states.select { |s| s.is_start? }.take(1).first
|
||||
end
|
||||
|
||||
def end_states
|
||||
@states.select { |s| s.is_end? }
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
18
lib/working_class/state_machine/transition.rb
Normal file
18
lib/working_class/state_machine/transition.rb
Normal file
@ -0,0 +1,18 @@
|
||||
module WorkingClass
|
||||
module StateMachine
|
||||
class Transition
|
||||
|
||||
attr_reader :from, :to, :regex
|
||||
|
||||
def initialize(from, to, regex)
|
||||
@from = from
|
||||
@to = to
|
||||
@regex = regex
|
||||
end
|
||||
|
||||
def to_s
|
||||
"#{from.to_s} -[#{regex.to_s}]-> #{to.to_s}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -1,91 +1,18 @@
|
||||
module WorkingClass
|
||||
|
||||
# A basic represantation of a Task
|
||||
#
|
||||
# @attr_reader name [String] the task name
|
||||
# @attr_reader is_finished [Boolean] true if the task is finished
|
||||
# @attr_reader date [Date] the day the task is due
|
||||
# @attr_reader reminder [DateTime] a DateTime with a reminder
|
||||
class Task
|
||||
attr_accessor :is_finished, :name, :reminder
|
||||
|
||||
attr_reader :name
|
||||
attr_reader :date
|
||||
attr_reader :reminder
|
||||
attr_reader :is_finished
|
||||
|
||||
alias :is_finished? :is_finished
|
||||
alias :finished? :is_finished
|
||||
|
||||
# Initializes a new task object with a name, and options
|
||||
#
|
||||
# @param name [String] the task name
|
||||
# @param options [Hash] an options hash
|
||||
#
|
||||
# @option options [Boolean] :is_finished (false) true if the task is finished
|
||||
# @option options [Date] :date (nil) the date when the task is due
|
||||
# @option options [DateTime] :reminder (nil )a DateTime with a reminder
|
||||
#
|
||||
def initialize name, options = {}
|
||||
options = {is_finished: false, date: nil, reminder: nil}.merge(options)
|
||||
def initialize(is_finished, name, reminder = nil)
|
||||
@is_finished = is_finished
|
||||
@name = name
|
||||
@is_finished = options[:is_finished]
|
||||
@date = options[:date]
|
||||
@reminder = options[:reminder]
|
||||
@reminder = reminder
|
||||
end
|
||||
|
||||
# Returns true if the task is upcoming
|
||||
#
|
||||
# A Task without a date is always upcoming.
|
||||
# @todo add example
|
||||
#
|
||||
# A finished task is never upcoming.
|
||||
# @todo add example
|
||||
#
|
||||
# @return [Boolean] true if the task is upcoming
|
||||
def is_upcoming
|
||||
if @is_finished
|
||||
false
|
||||
elsif !@date.nil?
|
||||
Date.today <= @date
|
||||
else
|
||||
true
|
||||
end
|
||||
def has_reminder
|
||||
reminder != nil
|
||||
end
|
||||
|
||||
alias :is_upcoming? :is_upcoming
|
||||
alias :upcoming? :is_upcoming
|
||||
|
||||
# Returns true if the task is due tomorrow
|
||||
#
|
||||
# A Task without a date is always due tomorrow.
|
||||
# @todo add example
|
||||
#
|
||||
# A finished task is never due tomorrow.
|
||||
# @todo add example
|
||||
#
|
||||
# @return [Boolean] true if the task is due tomorrow
|
||||
def is_tomorrow
|
||||
if @is_finished
|
||||
false
|
||||
elsif date.nil?
|
||||
true
|
||||
else
|
||||
Date.today + 1 == @date
|
||||
end
|
||||
end
|
||||
|
||||
alias :is_tomorrow? :is_tomorrow
|
||||
alias :tomorrow? :is_tomorrow
|
||||
|
||||
# Returns true if the Task is due today.
|
||||
# A finished task or a Task without a date is never today.
|
||||
#
|
||||
# @return [Boolean] true if the task is due today
|
||||
def is_today
|
||||
!@is_finished and @date == Date.today
|
||||
end
|
||||
|
||||
alias :is_today? :is_today
|
||||
alias :today? :is_today
|
||||
alias :has_reminder? :has_reminder
|
||||
alias :is_finished? :is_finished
|
||||
end
|
||||
end
|
||||
end
|
@ -1,66 +1,18 @@
|
||||
module WorkingClass
|
||||
|
||||
# A represantation of a Tasklist
|
||||
#
|
||||
# @attr_reader name [String] the name of the Tasklist
|
||||
# @attr_reader tasks [Array<WorkingClass::Task>] the tasks of the Tasklist
|
||||
class Tasklist
|
||||
attr_accessor :name, :tasks
|
||||
|
||||
attr_reader :name
|
||||
|
||||
attr_reader :tasks
|
||||
|
||||
# Initializes a new Tasklist with a name and optional Tasks
|
||||
#
|
||||
# @param name [String] the tasklist name
|
||||
# @param tasks [Array<WorkingClass::Task>] an array with Tasks
|
||||
# @return [WorkingClass::Tasklist] the actual Tasklist object
|
||||
#
|
||||
def initialize(name, tasks = [])
|
||||
@name = name
|
||||
@tasks = tasks
|
||||
@tasks = tass
|
||||
end
|
||||
|
||||
# Returns all the upcoming tasks
|
||||
#
|
||||
# @return [Array<WorkingClass::Task>] an Array with the upcoming tasks
|
||||
#
|
||||
def upcoming_tasks
|
||||
@tasks.select { |task| task.is_upcoming }
|
||||
end
|
||||
|
||||
# Returns all the tasks that are due today
|
||||
#
|
||||
# @return [Array<WorkingClass::Task>] an Array with the tasks due today
|
||||
#
|
||||
def tasks_today
|
||||
@tasks.select { |task| task.is_today }
|
||||
end
|
||||
|
||||
# Returns all the tasks that are due tomorrow
|
||||
#
|
||||
# @return [Array<WorkingClass::Task>] an Array with the tasks due tomorrow
|
||||
#
|
||||
def tasks_due_tomorrow
|
||||
@tasks.select { |task| task.is_tomorrow }
|
||||
end
|
||||
|
||||
# Returns all the finished tasks
|
||||
#
|
||||
# @return [Array<WorkingClass::Task>] an Array the finished tasks
|
||||
#
|
||||
def finished_tasks
|
||||
@tasks.select { |task| task.is_finished }
|
||||
end
|
||||
|
||||
# Returns all the unfinished tasks
|
||||
#
|
||||
# @return [Array<WorkingClass::Task>] an Array with the unfinished tasks
|
||||
#
|
||||
def unfinished_tasks
|
||||
@tasks.select { |task| !task.is_finished }
|
||||
tasks.select { |t| !t.is_finished }
|
||||
end
|
||||
|
||||
|
||||
def finished_tasks
|
||||
tasks.select { |t| t.is_finished }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -1,5 +1,3 @@
|
||||
module WorkingClass
|
||||
# The version of the gem.
|
||||
# We use semantic versioning
|
||||
VERSION = "0.2.1"
|
||||
end
|
||||
VERSION = "1.0.0"
|
||||
end
|
@ -1,3 +0,0 @@
|
||||
A cool todolist
|
||||
---
|
||||
[ ] My Task Number 1
|
@ -1,3 +0,0 @@
|
||||
Groceries List
|
||||
---
|
||||
[X] My finished Task
|
@ -1,4 +0,0 @@
|
||||
Development Todolist
|
||||
---
|
||||
[ ]{12.12.15} Release WorkingClass
|
||||
[x]{12.02.15} Yolo
|
@ -1,4 +0,0 @@
|
||||
Shopping List
|
||||
---
|
||||
[ ]{12.01.2015} Buy some Jeans
|
||||
[ ]{5.7.16}() Buy more Jeans
|
@ -1,5 +0,0 @@
|
||||
Another List Another Day
|
||||
---
|
||||
[X](15.2.15) Yolo
|
||||
[X]{16.2.15}(15.2.15 13:00) Yolo in the house
|
||||
|
@ -1,5 +0,0 @@
|
||||
Yeah more examples
|
||||
---
|
||||
[ ]{17.03.12}(-1) Twitter Downtime
|
||||
[ ]{12.5.15}(-2 5:00) Another Task
|
||||
[X]{12.3.15}(5:00) Remind me!!
|
@ -1,185 +0,0 @@
|
||||
require File.expand_path('../test_helper.rb', __FILE__)
|
||||
|
||||
class ParserTest < Minitest::Test
|
||||
|
||||
include WorkingClass
|
||||
|
||||
def load_example(example_name)
|
||||
path = File.expand_path("../examples/#{example_name}.txt", __FILE__)
|
||||
File.open(path, 'r').read()
|
||||
end
|
||||
|
||||
def test_initialize
|
||||
text = load_example('example_1')
|
||||
parser = Parser.new(text)
|
||||
assert_instance_of(Parser, parser)
|
||||
end
|
||||
|
||||
def test_to_a_simple_task
|
||||
text = load_example('example_1')
|
||||
parser = Parser.new(text)
|
||||
output = parser.to_h
|
||||
|
||||
assert_instance_of(Hash, output)
|
||||
assert_instance_of(Array, output[:tasks])
|
||||
assert_instance_of(Hash, output[:tasks].first)
|
||||
|
||||
assert_equal('A cool todolist', output[:name])
|
||||
assert_equal(1, output[:tasks_count])
|
||||
assert_equal('My Task Number 1', output[:tasks].first[:name])
|
||||
|
||||
assert(!output[:tasks].first[:is_finished])
|
||||
assert_nil(output[:tasks].first[:date])
|
||||
assert_nil(output[:tasks].first[:reminder])
|
||||
end
|
||||
|
||||
def test_to_a_simple_finished_task
|
||||
text = load_example('example_2')
|
||||
parser = Parser.new(text)
|
||||
output = parser.to_h
|
||||
|
||||
assert_instance_of(Hash, output)
|
||||
assert_instance_of(Array, output[:tasks])
|
||||
assert_instance_of(Hash, output[:tasks].first)
|
||||
|
||||
assert_equal('Groceries List', output[:name])
|
||||
assert_equal(1, output[:tasks_count])
|
||||
|
||||
assert_equal('My finished Task', output[:tasks].first[:name])
|
||||
assert(output[:tasks].first[:is_finished])
|
||||
assert_nil(output[:tasks].first[:date])
|
||||
assert_nil(output[:tasks].first[:reminder])
|
||||
end
|
||||
|
||||
def test_to_a_task_with_date
|
||||
text = load_example('example_3')
|
||||
parser = Parser.new(text)
|
||||
output = parser.to_h
|
||||
task_1 = output[:tasks].first
|
||||
task_2 = output[:tasks].last
|
||||
|
||||
assert_instance_of(Hash, output)
|
||||
assert_instance_of(Array, output[:tasks])
|
||||
assert_instance_of(Hash, task_1)
|
||||
assert_instance_of(Hash, task_2)
|
||||
|
||||
assert_equal('Development Todolist', output[:name])
|
||||
assert_equal(2, output[:tasks_count])
|
||||
|
||||
assert_equal('Release WorkingClass', task_1[:name])
|
||||
assert(!task_1[:is_finished])
|
||||
assert_equal(Date.new(2015, 12, 12), task_1[:date])
|
||||
assert_nil(task_1[:reminder])
|
||||
|
||||
assert_equal('Yolo', task_2[:name])
|
||||
assert(task_2[:is_finished])
|
||||
assert_equal(Date.new(2015, 2, 12), task_2[:date])
|
||||
assert_nil(task_2[:reminder])
|
||||
end
|
||||
|
||||
def test_to_a_task_with_full_date
|
||||
text = load_example('example_4')
|
||||
parser = Parser.new(text)
|
||||
output = parser.to_h
|
||||
task_1 = output[:tasks].first
|
||||
task_2 = output[:tasks].last
|
||||
|
||||
assert_instance_of(Hash, output)
|
||||
assert_instance_of(Array, output[:tasks])
|
||||
assert_instance_of(Hash, task_1)
|
||||
assert_instance_of(Hash, task_2)
|
||||
|
||||
assert_equal('Shopping List', output[:name])
|
||||
assert_equal(2, output[:tasks_count])
|
||||
|
||||
assert_equal('Buy some Jeans', task_1[:name])
|
||||
assert(!task_1[:is_finished])
|
||||
assert_equal(Date.new(2015, 1, 12), task_1[:date])
|
||||
assert_nil(task_1[:reminder])
|
||||
|
||||
assert_equal('Buy more Jeans', task_2[:name])
|
||||
assert(!task_2[:is_finished])
|
||||
assert_equal(Date.new(2016, 7 ,5), task_2[:date])
|
||||
assert_equal(DateTime.new(2016, 7, 5, 9, 0), task_2[:reminder])
|
||||
end
|
||||
|
||||
def test_to_a_task_with_absolute_reminder
|
||||
text = load_example('example_5')
|
||||
parser = Parser.new(text)
|
||||
output = parser.to_h
|
||||
task_1 = output[:tasks].first
|
||||
task_2 = output[:tasks].last
|
||||
|
||||
assert_instance_of(Hash, output)
|
||||
assert_instance_of(Array, output[:tasks])
|
||||
assert_instance_of(Hash, task_1)
|
||||
assert_instance_of(Hash, task_2)
|
||||
|
||||
assert_equal('Another List Another Day', output[:name])
|
||||
assert_equal(2, output[:tasks_count])
|
||||
|
||||
assert_equal('Yolo', task_1[:name])
|
||||
assert(task_1[:is_finished])
|
||||
assert_nil(task_1[:date])
|
||||
assert_equal(DateTime.new(2015, 2, 15, 9, 0) ,task_1[:reminder])
|
||||
|
||||
assert_equal('Yolo in the house', task_2[:name])
|
||||
assert(task_2[:is_finished])
|
||||
assert_equal(Date.new(2015, 2, 16), task_2[:date])
|
||||
|
||||
assert_equal(DateTime.new(2015, 2, 15, 13, 0), task_2[:reminder])
|
||||
end
|
||||
|
||||
def test_to_a_task_with_relative_reminder
|
||||
text = load_example('example_6')
|
||||
parser = Parser.new(text)
|
||||
output = parser.to_h
|
||||
task_1 = output[:tasks][0]
|
||||
task_2 = output[:tasks][1]
|
||||
task_3 = output[:tasks][2]
|
||||
|
||||
assert_instance_of(Hash, output)
|
||||
assert_instance_of(Array, output[:tasks])
|
||||
assert_instance_of(Hash, task_1)
|
||||
assert_instance_of(Hash, task_2)
|
||||
|
||||
assert_equal('Yeah more examples', output[:name])
|
||||
assert_equal(3, output[:tasks_count])
|
||||
|
||||
assert_equal('Twitter Downtime', task_1[:name])
|
||||
assert(!task_1[:is_finished])
|
||||
assert_equal(Date.new(2012, 3, 17), task_1[:date])
|
||||
assert_equal(DateTime.new(2012, 3, 16, 9, 0) ,task_1[:reminder])
|
||||
|
||||
assert_equal('Another Task', task_2[:name])
|
||||
assert(!task_2[:is_finished])
|
||||
assert_equal(Date.new(2015, 5, 12), task_2[:date])
|
||||
assert_equal(DateTime.new(2015, 5, 10, 5, 0) ,task_2[:reminder])
|
||||
|
||||
assert_equal('Remind me!!', task_3[:name])
|
||||
assert(task_3[:is_finished])
|
||||
assert_equal(Date.new(2015, 3, 12), task_3[:date])
|
||||
assert_equal(DateTime.new(2015, 3, 12, 5, 0), task_3[:reminder])
|
||||
end
|
||||
|
||||
def test_to_tasklist
|
||||
text = load_example('example_5')
|
||||
parser = Parser.new(text)
|
||||
tasklist = parser.to_tasklist
|
||||
task_1 = tasklist.tasks.first
|
||||
task_2 = tasklist.tasks.last
|
||||
|
||||
assert_instance_of(Tasklist, tasklist)
|
||||
assert_equal(2, tasklist.tasks.length)
|
||||
assert_equal("Another List Another Day", tasklist.name)
|
||||
|
||||
assert_equal('Yolo', task_1.name)
|
||||
assert(task_1.is_finished)
|
||||
assert_nil(task_1.date)
|
||||
|
||||
assert_equal('Yolo in the house', task_2.name)
|
||||
assert(task_2.is_finished)
|
||||
assert_equal(Date.new(2015, 2, 16), task_2.date)
|
||||
|
||||
end
|
||||
end
|
@ -4,125 +4,22 @@ class TaskTest < Minitest::Test
|
||||
|
||||
include WorkingClass
|
||||
|
||||
|
||||
def test_initialize
|
||||
task = Task.new "Buy oranges"
|
||||
task = Task.new(true, "Hello World")
|
||||
|
||||
assert_instance_of(Task, task)
|
||||
assert_equal "Buy oranges", task.name
|
||||
assert !task.is_finished?
|
||||
assert_nil task.date
|
||||
assert_nil task.reminder
|
||||
assert(task.is_finished?)
|
||||
assert_equal("Hello World", task.name)
|
||||
assert(!task.has_reminder?)
|
||||
assert_nil(task.reminder)
|
||||
end
|
||||
|
||||
def test_initialize_with_options
|
||||
task = Task.new "Buy even more oranges", :is_finished => true, :reminder => DateTime.new,
|
||||
:date => Date.new
|
||||
def test_initialize_with_reminder
|
||||
task = Task.new(false, "Good Morning", Time.new(2016, 10, 5))
|
||||
|
||||
assert_equal "Buy even more oranges", task.name
|
||||
assert task.is_finished?
|
||||
assert_instance_of DateTime, task.reminder
|
||||
assert_instance_of Date, task.date
|
||||
end
|
||||
|
||||
def test_is_upcoming_today
|
||||
task = Task.new "Futurama Marathon", :date => Date.today
|
||||
|
||||
assert(task.is_upcoming)
|
||||
end
|
||||
|
||||
def test_is_upcoming_tomorrow
|
||||
task = Task.new "Eat healthy", :date => Date.today + 1
|
||||
|
||||
assert(task.is_upcoming)
|
||||
end
|
||||
|
||||
def test_is_upcoming_yesterday
|
||||
task = Task.new "task lists are great", :date => Date.today - 1
|
||||
|
||||
assert(!task.is_upcoming)
|
||||
end
|
||||
|
||||
def test_is_upcoming_already_finished
|
||||
task = Task.new "Backup all the files", :is_finished => true, :date => Date.today
|
||||
|
||||
assert(!task.is_upcoming)
|
||||
end
|
||||
|
||||
def test_is_upcoming_already_finished_tomorrow
|
||||
task = Task.new "Eat chips", :is_finished => true, :date => Date.today + 1
|
||||
|
||||
assert(!task.is_upcoming)
|
||||
end
|
||||
|
||||
def test_is_upcoming_without_date
|
||||
task = Task.new "Backup all the files"
|
||||
|
||||
assert(task.is_upcoming)
|
||||
end
|
||||
|
||||
def test_is_today
|
||||
task = Task.new "Be a WUUH Girl", :date => Date.today
|
||||
|
||||
assert(task.is_today)
|
||||
end
|
||||
|
||||
def test_is_today_without_a_date
|
||||
task = Task.new "Never be a WUUH Girl"
|
||||
|
||||
assert(!task.is_today)
|
||||
end
|
||||
|
||||
def test_is_today_with_future_date
|
||||
task = Task.new "Never be a WUUH Girl again", :date => Date.today + 1
|
||||
|
||||
assert(!task.is_today)
|
||||
end
|
||||
|
||||
def test_is_tomorrow
|
||||
task = Task.new "Eat chips", :date => Date.today + 1
|
||||
|
||||
assert(task.is_tomorrow)
|
||||
end
|
||||
|
||||
def test_is_tomorrow_was_actually_yesterday
|
||||
task = Task.new "Eat chips", :date => Date.today - 1
|
||||
|
||||
assert(!task.is_tomorrow)
|
||||
end
|
||||
|
||||
def test_is_tomorrow_is_actually_next_week
|
||||
task = Task.new "Eat chips", :date => Date.today + 9
|
||||
|
||||
assert(!task.is_tomorrow)
|
||||
end
|
||||
|
||||
def test_is_tomorrow_without_a_date
|
||||
task = Task.new "Eat chips"
|
||||
|
||||
assert(task.is_tomorrow)
|
||||
end
|
||||
|
||||
def test_is_tomorrow_already_finished
|
||||
task = Task.new "Eat chips", :is_finished => true, :date => Date.today + 1
|
||||
|
||||
assert(!task.is_tomorrow)
|
||||
end
|
||||
|
||||
def test_alias_methods
|
||||
task = Task.new "my awesome task"
|
||||
|
||||
assert_respond_to(task, :is_finished?)
|
||||
assert_respond_to(task, :finished?)
|
||||
|
||||
assert_respond_to(task, :is_tomorrow?)
|
||||
assert_respond_to(task, :tomorrow?)
|
||||
|
||||
assert_respond_to(task, :is_upcoming?)
|
||||
assert_respond_to(task, :upcoming?)
|
||||
|
||||
assert_respond_to(task, :is_today?)
|
||||
assert_respond_to(task, :today?)
|
||||
assert(!task.is_finished)
|
||||
assert_equal("Good Morning", task.name)
|
||||
assert(task.has_reminder)
|
||||
assert_equal(Time.new(2016, 10, 5), task.reminder)
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -1,81 +0,0 @@
|
||||
require File.expand_path('../test_helper.rb', __FILE__)
|
||||
|
||||
class TasklistTest < Minitest::Test
|
||||
|
||||
include WorkingClass
|
||||
|
||||
def test_initialize
|
||||
tasklist = Tasklist.new('my list')
|
||||
|
||||
assert_instance_of(Array, tasklist.tasks)
|
||||
assert_equal('my list', tasklist.name)
|
||||
assert_equal(0, tasklist.tasks.length)
|
||||
end
|
||||
|
||||
def test_upcoming_tasks
|
||||
task_1 = Task.new("Task 1", :is_finished => true, :date => Date.today + 2)
|
||||
task_2 = Task.new("Task 2", :date => Date.today)
|
||||
task_3 = Task.new("Task 3", :is_finished => true, :date => Date.today - 1)
|
||||
tasks = [task_1, task_2, task_3]
|
||||
|
||||
tasklist = Tasklist.new("example_task_list", tasks)
|
||||
expected = [task_2]
|
||||
|
||||
assert_equal(expected, tasklist.upcoming_tasks)
|
||||
|
||||
end
|
||||
|
||||
def test_finished_tasks
|
||||
task_1 = Task.new("Task 1", :is_finished => true)
|
||||
task_2 = Task.new("Task 2")
|
||||
task_3 = Task.new("Task 3", :is_finished => true)
|
||||
tasks = [task_1, task_2, task_3]
|
||||
|
||||
tasklist = Tasklist.new("example_task_list", tasks)
|
||||
expected = [task_1, task_3]
|
||||
|
||||
assert_equal(expected, tasklist.finished_tasks)
|
||||
end
|
||||
|
||||
def test_tasks_due_tomorrow
|
||||
task_1 = Task.new("Task 1", :is_finished => true, :date => Date.today + 1)
|
||||
task_2 = Task.new("Task 2")
|
||||
task_3 = Task.new("Task 3", :date => Date.today + 2)
|
||||
task_4 = Task.new("Task 4", :date => Date.today + 1 )
|
||||
|
||||
tasks = [task_1, task_2, task_3, task_4]
|
||||
|
||||
tasklist = Tasklist.new("example_task_list", tasks)
|
||||
expected = [task_2, task_4]
|
||||
|
||||
assert_equal(expected, tasklist.tasks_due_tomorrow)
|
||||
end
|
||||
|
||||
def test_unfinished_tasks
|
||||
task_1 = Task.new("Task 1", :is_finished => true)
|
||||
task_2 = Task.new("Task 2")
|
||||
task_3 = Task.new("Task 3")
|
||||
task_4 = Task.new("Task 4", :is_finished => true)
|
||||
|
||||
tasks = [task_1, task_2, task_3, task_4]
|
||||
|
||||
tasklist = Tasklist.new("example_task_list", tasks)
|
||||
expected = [task_2, task_3]
|
||||
|
||||
assert_equal(expected, tasklist.unfinished_tasks)
|
||||
end
|
||||
|
||||
def test_tasks_today
|
||||
task_1 = Task.new("Task 1", :is_finished => true)
|
||||
task_2 = Task.new("Task 2", :date => Date.today)
|
||||
task_3 = Task.new("Task 3", :date => Date.today + 1)
|
||||
task_4 = Task.new("Task 4", :is_finished => true)
|
||||
|
||||
tasks = [task_1, task_2, task_3, task_4]
|
||||
|
||||
tasklist = Tasklist.new("example_task_list", tasks)
|
||||
expected = [task_2]
|
||||
|
||||
assert_equal(expected, tasklist.tasks_today)
|
||||
end
|
||||
end
|
@ -1,24 +0,0 @@
|
||||
require File.expand_path('../test_helper.rb', __FILE__)
|
||||
|
||||
class WorkingClassTest < Minitest::Test
|
||||
|
||||
def test_load_file
|
||||
path = File.expand_path('../examples/example_1.txt', __FILE__)
|
||||
tasklist = WorkingClass.load_file(path)
|
||||
|
||||
assert_instance_of(WorkingClass::Tasklist, tasklist)
|
||||
assert_equal(1, tasklist.tasks.length)
|
||||
assert_equal("A cool todolist", tasklist.name)
|
||||
end
|
||||
|
||||
def test_load
|
||||
path = File.expand_path('../examples/example_1.txt', __FILE__)
|
||||
string = File.open(path, 'r').read()
|
||||
tasklist = WorkingClass.load(string)
|
||||
|
||||
assert_instance_of(WorkingClass::Tasklist, tasklist)
|
||||
assert_equal(1, tasklist.tasks.length)
|
||||
assert_equal("A cool todolist", tasklist.name)
|
||||
end
|
||||
|
||||
end
|
@ -23,4 +23,5 @@ Gem::Specification.new do |spec|
|
||||
spec.add_development_dependency "bundler", "~> 1.7"
|
||||
spec.add_development_dependency "rake", "~> 10.0"
|
||||
spec.add_development_dependency "minitest", "~> 5.5.1"
|
||||
spec.add_development_dependency "pry"
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user