hiirc icon indicating copy to clipboard operation
hiirc copied to clipboard

Use traits instead of structs in listener

Open seppo0010 opened this issue 8 years ago • 5 comments

Hi! Thanks for the library.

I'm trying to use it on a project, but I'm having a really hard time writing unit tests for my listener implementation because I need an Irc struct, a Channel struct, etc. that are almost impossible to create for unit testing.

Is it possible for the Listener to receive traits instead of structs, so I can create my own mock implementations?

Thanks again!

seppo0010 avatar Apr 21 '16 14:04 seppo0010

Hey. This is an interesting idea. Here are my thoughts about this.

Rust does not support polymorphism, only generics. This means that the Listener trait must take all of its parameters as generics.

Also, Irc, Channel and ChannelUser have methods that would return those traits. Currently, the story of returning traits from a method isn't too great. We would have to Box the traits, and that would generate a lot of allocations. There is a RFC proposing a way to return traits that would make our life way easier. It has not been accepted yet.

A good side effect of this approach is that we could hide away the ugly Arcs everywhere.

I'm open to the idea. I think the library needs to re-designing. I'll wait to hear your thoughts.

sbstp avatar Apr 21 '16 20:04 sbstp

For my unit test purpose, I do not need all methods from the current struct to be present in the traits... only identification ones (Irc can be empty, Channel have name and ChannelUser nickname).

I don't think boxing the traits is needed for the method call, but I'm not sure.

About the overall design, I found the API quite intuitive 👍.

seppo0010 avatar Apr 21 '16 20:04 seppo0010

Boxing is required when you want to return something as a trait, because their size is unknown. Which methods do you want to test?

sbstp avatar Apr 21 '16 21:04 sbstp

No listener method returns anything... right?

For mock, I don't need any method on Irc (it can be an empty trait), Channel only needs to have name and ChannelUser only needs nickname. I think that's the minimum number of methods that would allow me basic testing.

seppo0010 avatar Apr 22 '16 11:04 seppo0010

I've tried a couple designs and none worked. This is a place where polymorphism would make it much easier. I couldn't get it to work with generics. If you have ideas, please let me know.

sbstp avatar Apr 28 '16 17:04 sbstp