From be544ae87dd5a36392253bec805d09bd44a32b6f Mon Sep 17 00:00:00 2001 From: Artem Darius Weber Date: Sat, 14 Dec 2024 13:32:21 +0300 Subject: [PATCH] feat: implement testing functions for Contact, Person, Student, and StudentShort classes; add sample student data --- lab2/main.rb | 112 +++++++++++++++++++++++----------------------- lab2/students.txt | 3 ++ 2 files changed, 60 insertions(+), 55 deletions(-) create mode 100644 lab2/students.txt diff --git a/lab2/main.rb b/lab2/main.rb index e714667..d439c1f 100644 --- a/lab2/main.rb +++ b/lab2/main.rb @@ -1,59 +1,61 @@ +require_relative 'contact' +require_relative 'person' require_relative 'student' require_relative 'student_short' +require_relative 'student_repository' + +def test_classes + contact = Contact.new(phone: '+798912465', telegram: '@example_user', email: 'test@example.com') + puts "Contact Info: #{contact.info}" + + person = Person.new(id: '1', git: 'https://github.com/example', contact: contact) + puts "Person Contact Info: #{person.contact_info}" + + + student = Student.new( + id: '2', + git: 'https://github.com/student_example', + contact: contact, + surname: 'Иванов', + name: 'Иван', + patronymic: 'Иванович' + ) + puts "Student Full Info: #{student.to_s}" + puts "Student Initials: #{student.surname_and_initials}" + puts "Student Contact Info: #{student.contact_info}" + + + student_short = StudentShort.from_student(student) + puts "Student Short Info: #{student_short.to_s}" + + + file_path = 'students.txt' + students = StudentRepository.read_from_txt(file_path) + puts "Read Students from File:" + students.each { |s| puts s.to_s } + + + output_path = 'output_students.txt' + StudentRepository.write_to_txt(output_path, students) + puts "Students written to file: #{output_path}" +end + + +def test_parsing + student_string = 'Иванов Иван Иванович | ID: 3 | Phone: +79876543210 | Telegram: @ivan_user | Email: ivan@example.com | Git: https://github.com/ivanov' + student = Student.from_string(student_string) + puts "Parsed Student: #{student.to_s}" + + short_string = 'Иванов И.И., Git: https://github.com/ivanov, Contact: Phone: +79876543210' + student_short = StudentShort.from_string('4', short_string) + puts "Parsed StudentShort: #{student_short.to_s}" +end + + +if __FILE__ == $0 + puts "=== Testing Classes ===" + test_classes -begin - student_string = "Норакет Норакет Норакет | ID: 2 | Phone: +1234567890 | Telegram: @noracat | Email: nora@example.com | Git: https://github.com/nora" - student_from_string = Student.from_string(student_string) - puts "Создан объект из строки:\n#{student_from_string}" - - - student1 = Student.new( - surname: 'Алексеевич', - name: 'Артем-Дариус', - patronymic: 'Вебер', - id: 1, - git: 'https://github.com/space-creator' - ) - - student1.set_contacts( - phone: '+79891242223', - telegram: '@alstroemeria22', - email: 'no-replay@djft.ru' - ) - - puts student1.get_info - puts "Surname and Initials: #{student1.surname_and_initials}" - puts "Git Info: #{student1.git_info}" - puts "Contact Info: #{student1.contact_info}" - - student2 = Student.new( - surname: 'Норакет', - name: 'Норакет', - patronymic: 'Фамилия' - ) - - student2.set_contacts( - phone: '+70000000000' - ) - - puts student1 - puts '-' * 40 - puts student2 - - - student_short_from_student = StudentShort.new(student1) - puts "StudentShort from Student object:" - puts "ID: #{student_short_from_student.id}" - puts "Surname and Initials: #{student_short_from_student.surname_initials}" - puts "Git: #{student_short_from_student.git}" - puts "Contact: #{student_short_from_student.contact}" - - student_short_from_string = StudentShort.from_string(5, 'Skye A.A., Git: https://github.com/skye, Contact: Phone: +4923467890') - puts "StudentShort from string:" - puts "ID: #{student_short_from_string.id}" - puts "Surname and Initials: #{student_short_from_string.surname_initials}" - puts "Git: #{student_short_from_string.git}" - puts "Contact: #{student_short_from_string.contact}" -rescue ArgumentError => e - puts "Ошибка: #{e.message}" + puts "\n=== Testing Parsing ===" + test_parsing end diff --git a/lab2/students.txt b/lab2/students.txt new file mode 100644 index 0000000..8713015 --- /dev/null +++ b/lab2/students.txt @@ -0,0 +1,3 @@ +Иванов Иван Иванович | ID: 1 | Phone: +12345678901 | Telegram: @ivanov_user | Email: ivanov@example.com | Git: https://github.com/ivanov +Петров Петр Петрович | ID: 2 | Phone: +98765432101 | Telegram: @petrov_user | Email: petrov@example.com | Git: https://github.com/petrov +Сидоров Сидор Сидорович | ID: 3 | Phone: +56789012345 | Telegram: @sidorov_user | Email: sidorov@example.com | Git: https://github.com/sidorov