Name transformation tests

Topics: Conventions
Jun 2, 2011 at 6:15 PM
Edited Jun 2, 2011 at 6:17 PM
To get a grip on the new name transformation code I knocked up some unit tests to cover all the input cases I could think of. I thought the output might be useful so its pasted in here. There are a couple of possible problems highlighted.
When I get a moment I'll fork and check in the unit tests they might be useful for others too.

Transform View to ViewModel
SomeNamespace.Customer
    -> SomeNamespace.CustomerModel
SomeNamespace.Views.Customer
    -> SomeNamespace.Views.CustomerModel
SomeNamespace.CustomerView
    -> SomeNamespace.CustomerViewModel
    -> SomeNamespace.Customer
SomeNamespace.Views.CustomerView
    -> SomeNamespace.ViewModels.CustomerViewModel
    -> SomeNamespace.ViewModels.Customer
SomeNamespace.CustomerPage
    -> SomeNamespace.CustomerPageViewModel
SomeNamespace.Views.CustomerPage
    -> SomeNamespace.ViewModels.CustomerPageViewModel

Transform View to ViewModel with interfaces
SomeNamespace.Customer
    -> SomeNamespace.CustomerModel
SomeNamespace.Views.Customer
    -> SomeNamespace.Views.CustomerModel
SomeNamespace.CustomerView
    -> SomeNamespace.CustomerViewModel
    -> SomeNamespace.Customer
    -> SomeNamespace.ICustomerViewModel
    -> SomeNamespace.ICustomer
SomeNamespace.Views.CustomerView
    -> SomeNamespace.ViewModels.CustomerViewModel
    -> SomeNamespace.ViewModels.Customer
    -> SomeNamespace.ViewModels.ICustomerViewModel
    -> SomeNamespace.ViewModels.ICustomer
SomeNamespace.CustomerPage
    -> SomeNamespace.CustomerPageViewModel
    -> SomeNamespace.ICustomerPageViewModel
    missing Customer and ICustomer (see CustomerView transforms)
 SomeNamespace.Views.CustomerPage
    -> SomeNamespace.ViewModels.CustomerPageViewModel
    -> SomeNamespace.ViewModels.ICustomerPageViewModel
    missing Customer and ICustomer (see CustomerView transforms)


Transform ViewModel to View
SomeNamespace.CustomerModel
    -> SomeNamespace.Customer
SomeNamespace.CustomerViewModel
    -> SomeNamespace.CustomerView
SomeNamespace.ViewModels.CustomerViewModel
    -> SomeNamespace.Views.CustomerView
SomeNamespace.CustomerPageViewModel
    -> SomeNamespace.CustomerPage
SomeNamespace.ViewModels.CustomerPageViewModel
    -> SomeNamespace.Views.CustomerPagePage


Transform ViewModel to View with Context
SomeNamespace.CustomerModel
    -> SomeNamespace.Customer
SomeNamespace.CustomerViewModel
    -> SomeNamespace.Customer.Context
SomeNamespace.ViewModels.CustomerViewModel
    -> SomeNamespace.Views.Customer.Context
SomeNamespace.CustomerPageViewModel
    -> SomeNamespace.Customer.Context
SomeNamespace.ViewModels.CustomerPageViewModel
    -> SomeNamespace.Views.CustomerPage.Context

Cheers,
Leaf.
Jun 2, 2011 at 9:44 PM
Edited Jun 2, 2011 at 10:31 PM

Yup. I see the problem. The last rule in the ViewModelLocator should be changed from:

            NameTransformer.AddRule
                (
                    @"(?<namespace>(.*\.)*)ViewModels\.(?<basename>[A-Za-z]\w*)(?<suffix>ViewModel$)",
                    @"${namespace}Views.${basename}Page",
                    @"(.*\.)*ViewModels\.[A-Za-z]\w*PageViewModel$"
                );

 

to this:

            NameTransformer.AddRule
                (
                    @"(?<namespace>(.*\.)*)ViewModels\.(?<basename>[A-Za-z]\w*)(?<suffix>PageViewModel$)", //<==add "Page"
                    @"${namespace}Views.${basename}Page",
                    @"(.*\.)*ViewModels\.[A-Za-z]\w*PageViewModel$"
                );

 

Jun 2, 2011 at 10:13 PM
Edited Jun 2, 2011 at 10:18 PM

As for the missing transforms, I intentionally omitted those based on information in this thread:

For interfaces we are going from the view to a View Model name. So something like:
ShellView => IShellViewModel, IShell, Shell or ShellViewModel
MainPage => MainPageViewModel, IMainPageViewModel (This is what VS names things by default, which is why I'd like the convention, as well as the reverse from MainPageViewModel => MainPage or MainPageView)
MainPageView => MainPageViewModel, IMainPageViewModel

 

Jun 3, 2011 at 7:11 AM
cb55555 wrote:

Yup. I see the problem. The last rule in the ViewModelLocator should be changed from:

            NameTransformer.AddRule
                (
                    @"(?<namespace>(.*\.)*)ViewModels\.(?<basename>[A-Za-z]\w*)(?<suffix>ViewModel$)",
                    @"${namespace}Views.${basename}Page",
                    @"(.*\.)*ViewModels\.[A-Za-z]\w*PageViewModel$"
                );

 

to this:

            NameTransformer.AddRule
                (
                    @"(?<namespace>(.*\.)*)ViewModels\.(?<basename>[A-Za-z]\w*)(?<suffix>PageViewModel$)", //<==add "Page"
                    @"${namespace}Views.${basename}Page",
                    @"(.*\.)*ViewModels\.[A-Za-z]\w*PageViewModel$"
                );

 


Rob, I sent you a pull request with this fix. I also added support for type names that begin with underscore. I know it's not that common, but it's allowed.

Coordinator
Jun 3, 2011 at 3:15 PM

I'll get it in as soon as I can. I'm travelling currently, so it might wait a bit.

Jun 4, 2011 at 12:03 AM

I checked in the unit tests. To minimise changes I rewrote them to use use the ViewLocator and ViewModelLocator types directly instead of extracting the rules out of those classes and testing just the name transforms.

I don't know if you have any interest in having tests in Caliburn.Micro but I'll keep adding to them in this fork as I find it a good way to get to know a code base.

Cheers,
Leaf. 

Jun 4, 2011 at 12:52 AM
leafgarland wrote:

I checked in the unit tests. To minimise changes I rewrote them to use use the ViewLocator and ViewModelLocator types directly instead of extracting the rules out of those classes and testing just the name transforms.

I don't know if you have any interest in having tests in Caliburn.Micro but I'll keep adding to them in this fork as I find it a good way to get to know a code base.

Cheers,
Leaf. 


I like the idea. I suggested earlier to have a console test application to do what you suggested here, but I forgot that CM is based on SL, and I didn't want to build an SL application just to run these tests.

Jun 4, 2011 at 12:57 AM

There is a WPF version of CM also, so the unit tests are just a standard .Net runtime class library which can be run by NUnit or Resharper etc.

Jun 14, 2011 at 3:39 AM

Can you add the tests for the following cases after the latest changes get pushed to the trunk?

  • Views.SomeNamespace.SomeView
  • Some.Views.Namespace.SomeView
  • ViewModels.SomeNamespace.SomeViewModel
  • Some.ViewModels.Namespace.SomeViewModel

 

Jun 15, 2011 at 5:21 PM

I can. Just waiting on those changes.